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?