0

I have written a REST Request mapper:

@RequestMapping(value = "/resttest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> receiveBody(@RequestBody String bodymsg,HttpServletRequest request)  {
    String header_value = request.getHeader("h_key");       
    return new ResponseEntity<String>(HttpStatus.OK);
}

I would like to send a POST method to this like that:

public static void sendpost() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("h_key","this is the value you need");
    HttpEntity<String> entity = new HttpEntity<String>("text body", headers);
    ResponseEntity<JSONObject> response = restTemplate
            .exchange("http://localhost:9800/resttest", HttpMethod.POST, entity,JSONObject.class);
}

But I get this error:

Unsupported Media Type

What is the problem with my sending method?

Probbee
  • 1
  • 1
  • Shouldn't your controller consume JSON? I've found it helpful to turn Spring's trace logging when I see errors like this, it'll tell you a lot more. – jongusmoe Oct 18 '16 at 20:34
  • The body that I sending is a String, the header is JSON. What should I do? I tried with REST GUI client sending data to controller and worked fine. The problem is with the sendpost() method. :( – Probbee Oct 18 '16 at 20:35
  • 1
    Your code shows that you are setting "h_key" in the header. The HttpEntity should encapsulate the JSON, the header should only provide the type. – Compass Oct 18 '16 at 20:40
  • @Compass Can you be more specific? What do you mean with: "header should only provide the type" By the way I have a workaround: If I change the Controller consumes part to: ALL_VALUE it works fine. – Probbee Oct 18 '16 at 20:43
  • Header contains message type. Body contains JSON. Example using Apache. Should be similar in Spring. HttpEntity can contain a StringEntity of JSON if necessary. http://stackoverflow.com/questions/7181534/http-post-using-json-in-java – Compass Oct 18 '16 at 20:45
  • 1
    change `consumes = MediaType.TEXT_PLAIN_VALUE` to `MediaType.APPLICATION_JSON` – Adolfo Oct 18 '16 at 21:28

1 Answers1

2

You set server side to accept plan text only, but your request sets content_type to "application/json"... means you are telling server side that you are sending JSON format, so that server side says "I don't support this media type" ( http error code 415 )

For what you said

I tried with REST GUI client sending data to controller and worked fine.

if you specify "content-type" in the REST GUI as "text/plain", probably you will get an same error code.

For

If I change the Controller consumes part to: ALL_VALUE it works fine.

, that's because now you are telling to server side code to take all the media type (Content-Type), so whatever value you set up in client side request, it doesn't matter anymore.

wonhee
  • 1,581
  • 1
  • 14
  • 24