如何发送数据(List <customer> customers)作为RS查询的输入(URI </customer>

时间:2012-05-25 11:36:20

标签: web-services rest post jersey jax-rs

我有一个JAX-RS Jersey WebService,我试图接受XML数据(或JSON)并返回一个String响应,显示传递的记录总数。

这是我的服务代码:

@Path("customers")
@Singleton
public class CustomersResource {
...
    @POST
    @Path("addall")
    @Produces("text/html")
    @Consumes(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public String addCustomers(List<Customer> customerList) {
        return "success : received " + customerList.size() ;
    }
    ...
}

这是我的客户代码:

public static void main(String[] args) {
    Client client = Client.create();

    WebResource webresource = client.resource( "http://localhost:8080/restdemo/services/customers/addall");

    String input  = "<customerList><customer><name>name1</name></customer><customer><name>name2</name></customer></customerList>";
    String response = webresource.type("application/xml").post( String.class, input );
    System.out.println(response);
}

错误:

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/json, was not found
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
    at com.sun.jersey.api.client.Client.handle(Client.java:648)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
    at restdemoclient.Main.main(Main.java:35)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/json, was not found
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
    ... 5 more

我将非常感谢任何有用的示例代码的帮助或指针。

1 个答案:

答案 0 :(得分:0)