0

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.

Raashith
  • 155
  • 3
  • 16
  • duplicate of [this Q](http://stackoverflow.com/questions/13108161/a-message-body-writer-for-java-class-not-found)? – rt2800 Jul 15 '15 at 08:56
  • Use `@Consumes(MediaType.APPLICATION_JSON)`Refer [this question][1] [1]: http://stackoverflow.com/questions/1725315/how-to-get-full-rest-request-body-using-jersey – Puneet Pandey Jul 15 '15 at 10:15
  • @PuneetPandey Made changes as you suggested. It is not working :( – Raashith Jul 15 '15 at 11:41

0 Answers0