JSON Jersey 自动序列化包装器问题

JSON Jersey Auto serialization Wrapper Problems

我有一个 POJO

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

和一个非常简单的服务

@Service("graph-service#default")
public class DefaultGraphService implements GraphService {
    public Response createGraph(Graph graph) {
        return Response.ok().build();
    }
}

它实现了一个非常简单的接口

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

    @Path( "create-graph" )
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createGraph(Graph graph);
}

我有一个简单的spring-context.xml设置如下

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd">

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

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

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

如果我在 tomcat 中启动服务,它工作正常,我可以 curl 一个像

这样的对象吗
{"v":2,"e":1}

没有任何困难。但是,如果我运行这个测试

@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);
    assertNotNull(result);
}

然后它失败了,因为有

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

如果我添加一个

@XmlRootElement

到 POJO 然后服务序列化图形对象但似乎发送

{"graph":{"e":1,"v",2}}

相反,我认为这是有道理的,但反序列化似乎仍然需要

{"e":1,"v":2}

当我收到错误消息时

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

我肯定遗漏了一些非常简单的东西。我希望将项目序列化为

{"v":2,"e":1}

但如果它们能正确反序列化,我可以接受根元素。即

{"graph":{"v":2,"e":1}}

看看您在服务器上的什么地方注册 JacksonJsonProvider

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

这是为了在服务器端处理JSON(反)序列化to/from POJO。但客户需要同样的支持。您可以使用重载

在客户端注册提供者。只需将 JacksonJsonProvider 添加到 providersList

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