简体   繁体   English

使用Jersey for REST Web服务发送HashMap

[英]Send a HashMap using Jersey for REST web service

I'm using Jersey for a REST webservice. 我正在将Jersey用于REST Web服务。 I want to send a HashMap to the server, but I have a problem. 我想将HashMap发送到服务器,但是有问题。 This code works fine if I change parameter of the method to String, but with a HashMap, it doesn't work : 如果我将方法的参数更改为String,则此代码可以正常工作,但是使用HashMap时,它将不起作用:

    ClientConfig config = new DefaultClientConfig();
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(config);

    URI uri = UriBuilder.fromUri("http://localhost:8081/serviceProxy_socle-01.00.00-SNAPSHOT/services/tableauDeBord/subventions").build();
    System.out.println(uri.toString());
    WebResource service = client.resource(uri);

    GenericType<TableauDeBordImpl<CoupsDPouceImpl>> informationsDossier = new GenericType<TableauDeBordImpl<CoupsDPouceImpl>>(){};


    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("rne", "0240984P");

    TableauDeBordImpl<CoupsDPouceImpl> content = service
    .accept(MediaType.APPLICATION_JSON)
    .entity(params, MediaType.APPLICATION_XML)
    .type(MediaType.APPLICATION_XML).post(informationsDossier);

This is the Stacktrace : 这是Stacktrace:

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.HashMap, and MIME media type, application/xml, was not found
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:148)
    at com.sun.jersey.api.client.Client.handle(Client.java:642)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:613)
    at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:503)
    at fr.liberaccess.pool.tester.Tester.testCoupsDpouce(Tester.java:63)
    at fr.liberaccess.pool.tester.Tester.<init>(Tester.java:39)
    at fr.liberaccess.pool.tester.Tester.main(Tester.java:188)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/xml, was not found
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:299)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:203)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:146)
    ... 7 more

Your need to create a MessageBodyWriter and annotate the class as a Provider. 您需要创建一个MessageBodyWriter并将该类注释为Provider。

You also need to tell Jersey about the new provider. 您还需要告知泽西岛有关新提供者的信息。

From the user guide. 从用户指南。

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(PlanetJAXBContextProvider.class);
Client c = Client.create(cc);

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

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