[英]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 中启动服务它工作正常我可以卷曲一个对象
{"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}
但是如果他们正确地反序列化,我可以接受一个根元素。 IE
{"graph":{"v":2,"e":1}}
查看您在服务器上注册JacksonJsonProvider
的位置?
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
这是为了在服务器端处理与 POJO 之间的 JSON(反)序列化。 但是客户需要同样的支持。 您可以使用重载
在客户端注册提供者。 只需将JacksonJsonProvider
添加到providers
List
中。
JAXRSClientFactory.create(baseUri, Service.class, Arrays.asList(new JacksonJsonProvider())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.