0
我有一个POJOJSON泽西自动序列化包装问题
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);
}
我有一个简单的弹簧-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}
但是,如果他们将正确deserialise我可以住在一个根元素。即
{"graph":{"v":2,"e":1}}
尝试使用重载['JAXRSClientFactory.create(字符串baseAddress,类 CLS,列表>提供商)'](https://cxf.apache.org/javadoc/latest/org/apache/cxf/JAXRS /客户/ JAXRSClientFactory。html#create%28java.lang.String,%20java.lang.Class,%20java.util.List%290),并将'JacksonJsonProvider'添加到提供者的列表 –
我不是100%确定的,(需要一些研究),但我可以给你一些提示。看起来你正在将杰克逊与原生JAXB混合在一起。我在tomcat上使用jsonProvider加载上下文并相应地进行解析。但在测试中,它可能无法获得jsonProvider,也许测试环境需要另一个配置。 因此,当您使用@XmlRootElement根据JAXB生成它时。 这些之间有点差别http://stackoverflow.com/questions/28895761/jackson-vs-jaxb-what-is-better-for-json?answertab=active#tab-top –
http:// codingexplained .com /编码/ java /忽略 - 无法识别 - json-fields-spring-jackson –