I am facing an exception on the JAX-RS Client side when I am trying to test JSON data exchange. I am using Jersey 2.10, JDK1.7, Tomcat7.0. Can someone please advise what steps, with examples, that I need to undertake to make this work. I have no ContextResolver, Provider, MessageBodyWriter/Reader configured on the client side as of now.
Following is the exception stack trace,
SEVERE: MessageBodyWriter not found for media type=application/json, type=class org.vedvrat.jaxrs.model.Customer, genericType=class org.vedvrat.jaxrs.model.Customer.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class org.vedvrat.jaxrs.model.Customer, genericType=class org.vedvrat.jaxrs.model.Customer.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:500)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:224)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:656)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:653)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:653)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:413)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:322)
at org.vedvrat.jaxrs.client.bbc.six.JSONRequestTest.main(JSONRequestTest.java:40)
You need to register one of the JSON JAX-RS providers. See a chapter dedicated to various JSON providers in Jersey User Guide. Basically, after picking a provider and adding appropriate modules on your class path, you need to register a provider in Jersey runtime, e.g. for Jackson it would be:
Client client = ClientBuilder.newClient().register(JacksonFeature.class);
See also Registering Resources and Providers in Jersey 2 to find out how this concept works.
you have to add genson-1.3.jar, it worked in my case
Related
I have a problem in catching the exceptions in my spring integration application.
Flow of operations in my application.
Http:inbound gateway which receives the request (error-channel defined to my custom error channel)
Service Activator for basic validations (Exceptions which are thrown from here are handled by error-channel defined on the GW)
splitter
Aggregator
Exceptions on my splitter or Aggregator are not handled by my error channel. why?
Steps taken:
I added a chain and included a header enricher and specified an error channel just before the splitter.
After this, any exception on my splitter is handled by my error channel mentioned in the header enricher.
<chain input-channel="invitations">
<header-enricher>
<error-channel ref="failed-invitations" />
</header-enricher>
<int:splitter ref="payloadSplitter" />
</chain>
But the same doesnt work when do the same on my Aggregator. why?
Whenever there is an exception in my code, it retries and gets executed more than one time. why?
I have a "errorChannel" defined which logs the exceptions. it doesnt work.
I know the thread is too old, but I was also facing a similar issue and found I declared error-channel in header-enricher but not provide 'overwrite="true"' as a parameter. And after providing 'overwrite="true"'it is working as needed. I am surprised why spring integration does not provide an overwrite=true by default.
Let us know this is what solution you did in your old code? So everyone can find out the solution for such a scenario.
I am trying to implement a basic 1 spout - 1 bolt Storm Topology. I have a Storm Bolt to make an HTTP Request using the Apache HttpClient (4.3.1). But, I get the following exception when I run it:
[main] ERROR org.apache.zookeeper.server.NIOServerCnxn - Thread Thread[main,5,main] died
java.lang.RuntimeException: java.io.NotSerializableException: org.apache.http.impl.client.InternalHttpClient
at backtype.storm.utils.Utils.serialize(Utils.java:56)
at backtype.storm.topology.TopologyBuilder.createTopology(TopologyBuilder.java:89)
at app.storm.StormTopology.main(StormTopology.java:26)
Caused by: java.io.NotSerializableException: org.apache.http.impl.client.InternalHttpClient
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at backtype.storm.utils.Utils.serialize(Utils.java:52)
... 2 more
Anyone else seen this? I know Storm itself uses the HTTP Client (4.1.1) internally. I tried to replace the internal library with the 4.3.1 version and I got another error:
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:727)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at app.storm.bolts.DataFetcherBolt.<init>(DataFetcherBolt.java:34)
at app.storm.StormTopology.main(StormTopology.java:18)
I got my answer from the Google Forum for the Storm project. Here is the link to that post:
https://groups.google.com/forum/#!topic/storm-user/vdfamKeR9Lk
The fix is to instantiate the HttpClient instance in the prepare method of the Bolt class.
I've put some good hours trying to get my grizzly webserver to produce JSON.
Generated from maven archetype using Intellij, details:
groupId untitled3
artifactId untitled3
version 1.0-SNAPSHOT
archetypeGroupId org.glassfish.jersey.archetypes
archetypeArtifactId jersey-quickstart-grizzly2
archetypeVersion 2.0-m05
All online examples enable JSON using
rc.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
However, this does not work in jersey 2.x, put method does not exist.
In the application Main class, there is instructions on uncommenting a line of code to get JSON to work. When i uncomment this line of code, the method used does not exist.
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in untitled2 package
final ResourceConfig rc = new ResourceConfig().packages("untitled2");
// uncomment the following line if you want to enable
// support for JSON on the service (you also have to uncomment
// dependency on jersey-media-json module in pom.xml)
// --
rc.addModule(org.glassfish.jersey.media.json.JsonJaxbModule);
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
When i try to serve a JSON response from a POJO object i get this error:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.example.JsonPost, genericType=class com.example.JsonPost.
I've no idea where to begin to look really. I've googled, plown through documentation and looked through user groups...
Make sure you uncomment the dependency in the POM. Then change the addModule call to this:
rc.addModules(new org.glassfish.jersey.media.json.JsonJaxbModule());
and be sure to include the correct #Produces annotation on your resource:
#Produces(MediaType.APPLICATION_JSON)
I am using RestEasy client with jackson providers and getting the above error
clientside code is:
ClientRequest request = new ClientRequest(url);
request.accept(MediaType.APPLICATION_JSON);
ClientResponse<String> response = request.get(String.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
BufferedReader br =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes())));
response.getEntity() is throwing ClientResponseFailure exception with the error being
Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String
My server side code is below:
#GET
#Path("/{itemId}")
#Produces(MediaType.APPLICATION_JSON)
public String item(#PathParam("itemId") String itemId) {
//custom code
return gson.toJSON(object);
}
You could try to add the following dependency to your maven pom.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
The problem actually is that RestEasy is unable to find the Jackson provider. I had to manually register it by the following code:
ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);
instance.registerProvider(ResteasyJacksonProvider.class);
Everything is working fine with this. But I am still unhappy with the solution as Resteasy is supposed to scan for the providers and register them automatically.
Client client = ClientBuilder.newBuilder().register(ResteasyJacksonProvider.class).build();
Just adding the line org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider into META-INF/services/javax.ws.rs.ext.Providers file, solves the problem.
This file is included into resteasy-jackson-providers.jar but same file is also included into another jar, restasy-jaxrs.jar and for an executable jar file, that use both these jars, they are not merged !!
I had a similar problem and I realized that the problem was related with the version of resteasy-jackson-provider. I just moved from 3.0.4.Final to 3.0.5.Final and the problem disappeared.
Additionally I also realized that if I change the third line to the following the result was the expected with no need to change the dependencies.
Response response = request.get(Object.class).toString();
Things that had made work my code were that I added:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>&{resteasy.version}</version>
</dependency>
Beside this I don't know why but it seems that resteasy not initializing providers when client were created, this means that it required to init them manually:
ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(instance);
instance.registerProvider(ResteasyJackson2Provider.class);
In general it's enough to run the client.
I don't know the full rationale behind that but we've hit the exact same problem (multiple times :P) and you need to change the MediaType to TEXT_PLAIN.
Or you can also let JAX-RS do the job for you: instead of doing gson.toJSON(object), simply return object and change your method signature to whatever class that is. JAX-RS (RESTEasy in your case) will automatically call Jackson (if it's properly configured) and serialize your object to json. Then on your client side you would request for that same class instead of String and everything should work on its own. I'm not particularly familiar with ClientRequest/Response so it might not work as I said; we use RESTEasy proxy functionality on the client side instead (see ProxyFactory). Nevertheless, JAX-RS/RESTEasy can do json serialize/deserialize automatically on the client side too so there's definetly a way.
If you really want to by-pass goodness of JAX-RS actually doing serialization for you (instead of manually calling GSON), use StreamingOutput (i.e. wrap outputter as StreamingOutput, return that object).
I can return simple text from my Pojo but it fails when I changed to return JSON. I have the correct annotations for produces and consumes in my methods and I checked that the content negotation between Jquery and the server is correct.
It seems to be that the conversion to JSON just fails. I copied the Jackson jars in webapps/tomee/lib. I also tried the lib folder of my application.
The error message when doing a GET:
No message body writer has been found for response class <myClass>
This error is fired by the JAXRSOut Intercepter class
The error when doing a Post is:
No message body reader has been found for request class <myClass>
This error is fired by the JAXRSUtils class
Thanks
I made this work by listing the JAX-RS providers in conf/system.properties file of TomEE. Then I added the jars to TomEE's lib folder. The documentation in the website states that one should drop the jars on webapp/tomee/lib, but that did not work for me.
conf/system.properties
openejb.cxf.jax-rs.providers = org.codehaus.jackson.jaxrs.JacksonJsonProvider, org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
Jars I dropped:
jackson-mapper-asl-1.9.9.jar
jackson-core-asl-1.9.9.jar
jackson-jaxrs-1.9.9
If you use jersey you could try Genson library http://code.google.com/p/genson/.
Latest version 0.92 provides automatic detection of json support in jax-rs web services with jersey.
You should only have to drop the jar and voila! :)