使用 Jersey for REST Web 服务发送 HashMap

发布于 2024-11-26 08:02:56 字数 2445 浏览 2 评论 0原文

我正在使用 Jersey 作为 REST Web 服务。 我想将 HashMap 发送到服务器,但遇到问题。如果我将方法的参数更改为 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);

这是 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

I'm using Jersey for a REST webservice.
I want to send a HashMap to the server, but I have a problem. This code works fine if I change parameter of the method to String, but with a HashMap, it doesn't work :

    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 :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-柠檬树下少年和吉他 2024-12-03 08:02:56

您需要创建一个 MessageBodyWriter 和将类注释为 Provider。

您还需要告知 Jersey 有关新提供商的信息。

来自用户指南。

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

Your need to create a MessageBodyWriter and annotate the class as a 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文