Here is the code sample:
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/punya/testJson/22");
Builder b = webResource.accept("application/json");
String s = "";
if (b != null) {
try {
s = b.get(String.class);
} catch (Exception ex) {
System.out.println("Exception:" + ex);
}
} else {
System.out.println("Return data is null");
}
System.out.println("Return string=" + s);
ResponseBuilder builder = Response.ok("SUCCESS");
return builder.build();
Monday, November 29, 2010
Sunday, November 28, 2010
Jersey - java.lang.AbstractMethodError
- Make sure the bean used in the resource method return has no arg constructor...otherwise appplication/json will not work.
- Changing the return type from application/json to text/plain solved one issue.
--------------
- Now there is AbstractMethodError. This is caused by the inclusion of both cxf and jersey libraries in the classpath.
- http://web.archiveorange.com/archive/v/ACbGtNIkWRe4LYpKxHnE
java.lang.AbstractMethodError: org.apache.cxf.jaxrs.provider.XMLBeansElementProvider.isWriteable(Ljava/lang/Class;Ljava/lang/reflect/Type;[Ljava/lang/annotation/Annotation;Ljavax/ws/rs/core/MediaType;)Z
- Removing cxf (even the one that has been renamed as z-cxf, solved the issue)works with both application/json and also plain text return.
- Now added cxf again by renaming it to z-cxf.
----------------
- But still the error is there for application/json return data
27 Nov 2010 22:30:25 PST ERROR [/].[RESTful Web Services] -- Servlet.service() for servlet RESTful Web Services threw exception
java.lang.AbstractMethodError
at com.sun.jersey.core.spi.factory.MessageBodyFactory._getMessageBodyReader(MessageBodyFactory.java:268)
at com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyReader(MessageBodyFactory.java:256)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:425)
at
Also the following.....
01 Dec 2010 05:03:43 PST ERROR [/].[RESTful Web Services] -- Servlet.service() for servlet RESTful Web Services threw exception
java.lang.AbstractMethodError: org.apache.cxf.jaxrs.provider.XMLBeansElementProvider.isWriteable(Ljava/lang/Class;Ljava/lang/reflect/Type;[Ljava/lang/annotation/Annotation;Ljavax/ws/rs/core/MediaType;)Z
at
This is fixed by including the following in web.xml (Note: remove extra space after <):
-------------------
Other useful articles read while dedugging thi issue:
- Now as per the comments at http://web.archiveorange.com/archive/v/ACbGtNIkWRe4LYpKxHnE
added the following to web.xml:
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
rest.resources;rest.providers
But the resources and providers is just an example....so it failed. Doing it again now....but no luck
- http://jersey.576304.n2.nabble.com/Using-Jersey-1-x-for-JAX-RS-along-CXF-2-1-x-for-JAX-WS-td2490569.html
The issue is that CXF also contains an implementation of JAX-RS.
You need to ensure if you want to use Jersey that Jersey's
RuntimeDelegate is chosen (as described in the email), or iyou might
be able to place jersey jars before CXF jars in the class path.
- THe following article says to register with RunTImeDelegateImpl to solve the problem:
http://markmail.org/search/?q=list%3Anet.java.dev.jersey.users+AbstractMethodError#query:list%3Anet.java.dev.jersey.users%20AbstractMethodError+page:1+mid:x3y2gjfywvtscmzd+state:results
- Try this later http://blogs.sun.com/japod/entry/building_simple_jersey_web_app
Added the following to web.xml
----------
http://jersey.576304.n2.nabble.com/AbstractMethodError-on-requesting-resource-td2307443.html
I assumed the CXF version is getting loaded first because it appears
first in the classpath and therefore the ServiceLoader listing (or
something similar), so I renamed 'cxf-2.1.4.jar' to 'z-cxf-2.1.4.jar'.
It actually worked!
It looks like I can fart around with providing the correct
javax.ws.rs.ext.RuntimeDelegate implementation with a properties file
somewhere or other ($java.home/lib -- really? ugh), so I'll try that.
But I wanted to post this so nobody wasted time on my earlier message......
........
Another approach might be to register the Jersey
RuntimeDelegateImpl in that listener explicitly:
RuntimeDelegate.setInstance(new
com.sun.jersey.server.impl.provider.RuntimeDelegateImpl());
You could do this using your own servlet context listener. Or override
the:
com.sun.jersey.spi.container.servlet.ServlerContainer
and in the constructor set the runtime delegate instance.
Would it be possible to try the above? if it works we could support a
special feature, as an init-param, to explicitly set the runtime
delegate. ............
-------------
- Changing the return type from application/json to text/plain solved one issue.
--------------
- Now there is AbstractMethodError. This is caused by the inclusion of both cxf and jersey libraries in the classpath.
- http://web.archiveorange.com/archive/v/ACbGtNIkWRe4LYpKxHnE
java.lang.AbstractMethodError: org.apache.cxf.jaxrs.provider.XMLBeansElementProvider.isWriteable(Ljava/lang/Class;Ljava/lang/reflect/Type;[Ljava/lang/annotation/Annotation;Ljavax/ws/rs/core/MediaType;)Z
- Removing cxf (even the one that has been renamed as z-cxf, solved the issue)works with both application/json and also plain text return.
- Now added cxf again by renaming it to z-cxf.
----------------
- But still the error is there for application/json return data
27 Nov 2010 22:30:25 PST ERROR [/].[RESTful Web Services] -- Servlet.service() for servlet RESTful Web Services threw exception
java.lang.AbstractMethodError
at com.sun.jersey.core.spi.factory.MessageBodyFactory._getMessageBodyReader(MessageBodyFactory.java:268)
at com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyReader(MessageBodyFactory.java:256)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:425)
at
Also the following.....
01 Dec 2010 05:03:43 PST ERROR [/].[RESTful Web Services] -- Servlet.service() for servlet RESTful Web Services threw exception
java.lang.AbstractMethodError: org.apache.cxf.jaxrs.provider.XMLBeansElementProvider.isWriteable(Ljava/lang/Class;Ljava/lang/reflect/Type;[Ljava/lang/annotation/Annotation;Ljavax/ws/rs/core/MediaType;)Z
at
This is fixed by including the following in web.xml (Note: remove extra space after <):
<>
<>RESTful Web Services
<>com.sun.jersey.spi.container.servlet.ServletContainer
<>
<>com.sun.jersey.config.property.resourceConfigClass
<>com.sun.jersey.api.core.PackagesResourceConfig
< /init-param>
<>
<>com.sun.jersey.config.property.packages
<>com.punya.ws.resources
< /init-param>
< /servlet>
-------------------
Other useful articles read while dedugging thi issue:
- Now as per the comments at http://web.archiveorange.com/archive/v/ACbGtNIkWRe4LYpKxHnE
added the following to web.xml:
But the resources and providers is just an example....so it failed. Doing it again now....but no luck
- http://jersey.576304.n2.nabble.com/Using-Jersey-1-x-for-JAX-RS-along-CXF-2-1-x-for-JAX-WS-td2490569.html
The issue is that CXF also contains an implementation of JAX-RS.
You need to ensure if you want to use Jersey that Jersey's
RuntimeDelegate is chosen (as described in the email), or iyou might
be able to place jersey jars before CXF jars in the class path.
- THe following article says to register with RunTImeDelegateImpl to solve the problem:
http://markmail.org/search/?q=list%3Anet.java.dev.jersey.users+AbstractMethodError#query:list%3Anet.java.dev.jersey.users%20AbstractMethodError+page:1+mid:x3y2gjfywvtscmzd+state:results
- Try this later http://blogs.sun.com/japod/entry/building_simple_jersey_web_app
Added the following to web.xml
com.sun.jersey.config.property.resourceConfigClass
com.sun.jersey.api.core.PackagesResourceConfig
com.sun.jersey.config.property.packages
com.example.my
Hurray, adding the above to web.xml worked fine...but still without cxf library. Now trying with cxf....----------
http://jersey.576304.n2.nabble.com/AbstractMethodError-on-requesting-resource-td2307443.html
I assumed the CXF version is getting loaded first because it appears
first in the classpath and therefore the ServiceLoader listing (or
something similar), so I renamed 'cxf-2.1.4.jar' to 'z-cxf-2.1.4.jar'.
It actually worked!
It looks like I can fart around with providing the correct
javax.ws.rs.ext.RuntimeDelegate implementation with a properties file
somewhere or other ($java.home/lib -- really? ugh), so I'll try that.
But I wanted to post this so nobody wasted time on my earlier message......
........
Another approach might be to register the Jersey
RuntimeDelegateImpl in that listener explicitly:
RuntimeDelegate.setInstance(new
com.sun.jersey.server.impl.provider.RuntimeDelegateImpl());
You could do this using your own servlet context listener. Or override
the:
com.sun.jersey.spi.container.servlet.ServlerContainer
and in the constructor set the runtime delegate instance.
Would it be possible to try the above? if it works we could support a
special feature, as an init-param, to explicitly set the runtime
delegate. ............
-------------
Saturday, November 27, 2010
XmlRootElement and application/json
When the bean is defined without@XmlRootElement annotation(import javax.xml.bind.annotation.XmlRootElement;), and if the web service consumes application/json for that object, then the following error thrown:
-------------------
SEVERE: A message body reader for Java class com.punya.beans.E
mailTest, and Java type class com..punya.beans.EmailTest, and M
IME media type application/json was not found
Nov 27, 2010 4:14:43 AM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: The registered message body readers compatible with the MIME media type
are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
-------------------
SEVERE: A message body reader for Java class com.punya.beans.E
mailTest, and Java type class com..punya.beans.EmailTest, and M
IME media type application/json was not found
Nov 27, 2010 4:14:43 AM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: The registered message body readers compatible with the MIME media type
are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
Tuesday, November 23, 2010
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.replacePath(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
When you see this error, it means UriBuilder is being loaded from other libraries like cxf-2.1.3.jar. Just rename it to z-cxf-2.1.3.jar. It should work fine.
Subscribe to:
Posts (Atom)