我有一个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}
但如果他们能正确反序列化,我可以使用根元素。即。
{"graph":{"v":2,"e":1}}
答案 0 :(得分:1)
查看您在服务器上注册JacksonJsonProvider
的位置?
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
这是为了处理服务器端POJO的JSON(de)serizalization。但客户需要相同的支持。您可以使用重载
在客户端注册提供商。只需将JacksonJsonProvider
添加到List
的{{1}}。
providers