1
我正在使用Jersey進行REST Web服務。 我想發送一個HashMap到服務器,但我有一個問題。此代碼工作正常,如果我改變方法爲String的參數,但有一個HashMap,它不工作:使用Jersey發送REST Web服務的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);
這是堆棧跟蹤:
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
Thnx爲您的答案。我有一個註釋爲另一個返回類型的Provider的類。我可以有多個註解爲Provider的類? –
@taichimaro我相信你可以擁有儘可能多的供應商,只要你想。澤西島將使用它可以找到的第一個提供商接受給定的輸入和媒體類型。 –