James Robinson James Robinson - 1 year ago 66
Java Question

JSON Jersey Auto serialization Wrapper Problems

I have a POJO

public class Graph {
public int v;
public int e;

and a very simple service

public class DefaultGraphService implements GraphService {
public Response createGraph(Graph graph) {
return Response.ok().build();

which implements a very simple interface

@Path( "graph-service" )
public interface GraphService {

@Path( "create-graph" )
public Response createGraph(Graph graph);

I have a simple spring-context.xml set up as follows

<beans xmlns="http://www.springframework.org/schema/beans"

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<context:component-scan base-package="me.jamesphiliprobinson.graphs"/>

<jaxrs:server id="testServices" address="/testServices">
<ref bean="graph-service#default"/>
<ref bean="jsonProvider"/>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

If I spin the service up in tomcat it works fine can I can curl an object like


Without any difficulty. However, if I run this test

public void testCreateGraph() {
UncertaintyGraphService service =
JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testServices/",
GraphService.class );
Graph graph = new Graph();
graph.e = 1;
graph.v = 2;
Response result = service.createGraph(graph);

Then it fails as there is

No message body writer has been found for class : class me.jamesphiliprobinson.graphs.Graph, ContentType : application/json

If I add an


to the POJO then the service serializes the graph object but seems to send


Instead which I can see making sense but the deserialization seems to still expect


As I get the error

WARNING: WebApplicationException has been caught : Unrecognized field "graph" (Class me.jamesphiliprobinson.graphs.Graph), not marked as ignorable

I'm surely missing something incredibly simple. I would prefer for the items to serialise to


But if they will deserialise correctly I can live with a root element. i.e.


Answer Source

See where you are registering the JacksonJsonProvider on the server?

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

This is to handle JSON (de)serizalization to/from POJO on the server side. But the client needs the same support. You can use the overloaded

to register the provider on the client side. Just add the JacksonJsonProvider to the List of providers.

JAXRSClientFactory.create(baseUri, Service.class, Arrays.asList(new JacksonJsonProvider())