I want to pass HashMap to Restful API web service. I don't know that how to implement it. Please help me and I am very new to this topic.
Restful API code below:
@Path("/restService")
public class RestService {
@PUT
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response printMessage(JAXBElement<HashMap<String, String>> input) {
Map<String, String> m = input.getValue();
String result = "Your input is " + m.get("from");
return Response.status(200).entity(result).build();
}
}
Client Code :
String url = "http://<IP_ADDRESS_OF_SERVER>/SampleRestService/rest/restService/";
Client restClient = Client.create();
Map<String, String> map = new HashMap<String, String>();
map.put("from", "Java");
WebResource webResource = restClient.resource(url);
ClientResponse clientResponse = webResource.put(ClientResponse.class, map);
System.out.println(clientResponse.getEntity(String.class));
And getting below exception:
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/octet-stream, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.put(WebResource.java:223)
at com.alcatel.client.RestClient.main(RestClient.java:37)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/octet-stream, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
... 4 more
Please help me to solve this.