I'm trying to use jersey with my own json MessageBodyReader/MessageBodyWriter (as I am not use @XmlRootElement... annotations on my domain classes).
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class MyGsonMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
...
}
Jersey uses this class as messagebodywriter (as it stops at breakpoint in the implemented method writeTo). Hovewer it does not see this class as messagebodyreader (and even when I break up this class to the separate implementations of the messagebodyreader/messagebodywriter it still refuses to use my messagebodyreader).
The testing code looks as follows (jersey-grizzly):
final Greeting greeting = resource.path("/greeting")
.queryParam("name", name)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.get(Greeting.class);
The error I got looks as follows:
A message body reader for Java class test.Greeting, and Java type class test.Greeting, and MIME media type application/json was not found
I'm wondering what kind of magic is required for writing own MessageBodyReader?
After a while I found a root cause of the issue. My implementation of MessageBodyReader/Writer is OK (and I it works fine with RESTlet), but IF YOU USE JerseyTest, DO NOT FORGET TO ADD YOUR MessageBodyReader/Writer to it's ClientConfig:
/**
* Creates custom REST client config which is mandatory since we don't use any JSON providers.
* @return Jersey Client Config with the required classes to read/write in(out)coming data.
*/
private static ClientConfig createClientConfig() {
final ClientConfig config = new DefaultClientConfig();
config.getClasses().add(GsonMessageBodyHandler.class);
config.getClasses().add(GsonAwareContextResolver.class);
return config;
}
/**
* Public ctor
* @throws com.sun.jersey.test.framework.spi.container.TestContainerException On error
*/
public MyRestExposureTest() throws TestContainerException {
super(new WebAppDescriptor.Builder("my.rest.package")
.clientConfig(createClientConfig())
.contextPath("/")
.build());
}
Otherwise your client code would be unable to read/write your POJOs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With