简体   繁体   English

无法使用JAX-RS(CXF)解组JSON到对象

[英]Cannot unmarshal JSON-to-object with JAX-RS (CXF)

Using latest version of CXF and other dependencies, I'm trying to unit test web services which produces and consumes JSON. 使用最新版本的CXF和其他依赖项,我试图对生成和使用JSON的Web服务进行单元测试。 Test works when method argument is String, but fails when argument is custom object. 当方法参数为String时测试有效,但当参数为自定义对象时测试失败。 Following is the sample resource: 以下是示例资源:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("testPost")
public Employee getPostTest(Employee data) {
    return data;
}

Following is the excerpt of a test case 以下是一个测试案例的摘录

    List<Object> providers = new ArrayList<Object>();
    providers.add(new JacksonJaxbJsonProvider());
    WebClient client = WebClient.create(ENDPOINT_ADDRESS, providers);
    client.accept("application/json");
    client.type("application/json");
    client.path("users/testPost/");
    Employee e = new Employee();
    e.setName("Test");
    Response r = client.post(e);

Last line of the above code throws the following exception: 上面的代码的最后一行抛出以下异常:

Dec 05, 2016 2:46:50 AM org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem SEVERE: No message body reader has been found for class com.finity.model.Employee, ContentType: application/json Dec 05, 2016 2:46:50 AM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse WARNING: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1315) at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:826) at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:789) 2016年12月5日2:46:50 org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem SEVERE:找不到类com.finity.model.Employee的消息正文阅读器,ContentType:application / json 2016年12月5日上午2:46:50 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper做出响应警告:javax.ws.rs.WebApplicationException:HTTP 415 org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils。 java:1315),位于org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:826),位于org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:789)

Tried the read using JSONParser but no go: 使用JSONParser尝试读取,但没有成功:

    Employee e = new Employee();
    e.setName("Test");
    Response r = client.post(e);

    MappingJsonFactory factory = new MappingJsonFactory();
    JsonParser parser = factory.createJsonParser((InputStream)r.getEntity());

You need to configure the JSON provider. 您需要配置JSON提供程序。 For example Jackson 例如杰克逊

 <jaxrs:providers>
   <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
  </jaxrs:providers>

or JacksonJaxbJsonProvider (when working with JAXB beans) JacksonJaxbJsonProvider (使用JAXB Bean时)

It is needed also to add the maven dependency if you do not have it. 如果还没有,还需要添加Maven依赖项。 org.codehaus.jackson jackson-jaxrs 1.9.0 org.codehaus.jackson jackson-jaxrs 1.9.0

See CXF JSON support 请参阅CXF JSON支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM