首页 > apache CXF 谁用过?如何将POST方法传进来的json数据自动变成对象呢?

apache CXF 谁用过?如何将POST方法传进来的json数据自动变成对象呢?

java
@PUT @Path("/customers/") public Response updateCustomer(Customer customer) { System.out.println("----invoking updateCustomer, Customer name is: " + customer.getName()); Customer c = customers.get(customer.getId()); Response r; if (c != null) { customers.put(customer.getId(), customer); r = Response.ok().build(); } else { r = Response.notModified().build(); } return r; }

这是我在apache cxf上面的sample上看见的。

客户端传递了这样一个xml文件到服务器:

xml<Customer>
  <name>Jack</name>
</Customer> 

这样,apache cxf 就自动将 这个xml文件转成了对象。(参数是Customer customer)
如果是传递一个json对象,怎么做才能自动的转换成一个对象呢?是不是apache cxf里面有一个拦截器可以做到这一点?


简单来说你需要提供一个json处理器,比如jackson

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

然后在方法上加上注解:

@Consumes(MediaType.APPLICATION_JSON)

提示cxf处理json。

详细可以参考:
链接描述

【热门文章】
【热门文章】