gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:24:56 25 4
gpt4 key购买 nike

我有一个 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();
}
}

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

@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" />

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

在客户端注册提供程序。只需添加 JacksonJsonProviderListproviders .

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

关于java - JSON Jersey 自动序列化包装器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com