141

I didn't find any example how to solve my problem, so I want to ask you for help. I can't simply send POST request using RestTemplate object in JSON

Every time I get:

org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

I use RestTemplate in this way:

...
restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
list.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(list);
...
Payment payment= new Payment("Aa4bhs");
Payment res = restTemplate.postForObject("http://localhost:8080/aurest/rest/payment", payment, Payment.class);

What is my fault?

arghtype
  • 4,376
  • 11
  • 45
  • 60
Johnny B
  • 1,419
  • 2
  • 10
  • 3
  • 1
    @troyfolger the url is no longer valid – Noremac May 13 '15 at 21:25
  • Thanks - this link is working as of this writing: https://spring.io/guides/gs/consuming-rest/ – troyfolger Sep 10 '15 at 17:42
  • 1
    To address the specific OP issue, above, you are probably missing an HTTP header with appropriate content type, see the answer from morganw09dev below. – troyfolger Sep 10 '15 at 17:45
  • These issues are mostly related to the Server API configuration. You test the Server API using a Standalone client (like Postman ) and replicate the same headers in your request. At least in my case that did the Trick. – Linus Mar 20 '16 at 09:08
  • 2
    @Johnny B, if this has been answered please mark the answer – Vishal Aug 08 '17 at 13:11
  • I am also facing the same issue. Can you look into it. – user3368288 Jan 05 '18 at 13:52

15 Answers15

180

This technique worked for me:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
            
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
ResponseEntity<String> response = restTemplate.put(url, entity);
starball
  • 20,030
  • 7
  • 43
  • 238
114

I ran across this problem when attempting to debug a REST endpoint. Here is a basic example using Spring's RestTemplate class to make a POST request that I used. It took me quite a bit of a long time to piece together code from different places to get a working version.

RestTemplate restTemplate = new RestTemplate();

String url = "endpoint url";
String requestJson = "{\"queriedQuestion\":\"Is there pain in your hand?\"}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
String answer = restTemplate.postForObject(url, entity, String.class);
System.out.println(answer);

The particular JSON parser my rest endpoint was using needed double quotes around field names so that's why I've escaped the double quotes in my requestJson String.

Morgan Kenyon
  • 3,072
  • 1
  • 26
  • 38
  • can u please help me on this http://stackoverflow.com/questions/42240927/how-to-search-via-json-in-elastic-search-using-spring-resttemplate-in-android – FaisalAhmed Feb 16 '17 at 10:16
  • 1
    Can Spring use the message converters to automatically convert the Java Object to json like it did in Restful API with RestTemplate? – fall Aug 10 '17 at 10:21
  • 1
    Setting media type to APPLICATION_JSON is the key to resolve the problem. – Pete T Jun 07 '18 at 15:43
  • I resolved my problem using HttpEntity entity = new HttpEntity(requestJson,headers); this line – Onic Team Apr 01 '19 at 09:42
101

I've been using rest template with JSONObjects as follow:

// create request body
JSONObject request = new JSONObject();
request.put("username", name);
request.put("password", password);

// set headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(request.toString(), headers);

// send request and parse result
ResponseEntity<String> loginResponse = restTemplate
  .exchange(urlString, HttpMethod.POST, entity, String.class);
if (loginResponse.getStatusCode() == HttpStatus.OK) {
  JSONObject userJson = new JSONObject(loginResponse.getBody());
} else if (loginResponse.getStatusCode() == HttpStatus.UNAUTHORIZED) {
  // nono... bad credentials
}
Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • Thanks - the JSONObject toString method was useful for me, it helped me get my JSONString accurate. – Simon Dec 21 '15 at 20:02
  • 1
    How to developed above code for this : curl -vvv -X POST "http://localhost:8080/SillyService_SRC/oauth/token?grant_type=password&client_id=my-trusted-client&username=admin&password=password" ? – PAA Aug 19 '16 at 11:23
  • @Mikael Lepistö How can i retrieve these parameters from json at server end?? – KJEjava48 Feb 15 '17 at 10:35
  • @KJEjava48 I don't understand what do you mean... this is server side code in the response. If you are thinking how to parse json response, it depends on framework that you are using. – Mikael Lepistö Feb 15 '17 at 15:17
  • @MikaelLepistö I mean how to parse json response in the other end including how to receive the response in java??You have posted only the code for one end(ie, server side). – KJEjava48 Feb 16 '17 at 12:51
  • @KJEjava48 If you get request body containing JSON string from your choice of HTTP server you can convert it to JSONObject like this: `JSONObject obj = new JSONObject(jsonString);`, but pretty much every java web server framework has some better way to do it. – Mikael Lepistö Feb 16 '17 at 18:29
  • Can we let Spring convert the java object to json for us with message converters behind the screen like Rest APIs? – fall Aug 10 '17 at 10:34
  • @fall I don't suppose Spring does that automatically, probably you need to configure some service for sending requests and write DTOs and use them if you want more automation. Maybe someone who know Spring better than me can have better answer... – Mikael Lepistö Aug 14 '17 at 07:11
14

As specified here I guess you need to add a messageConverter for MappingJacksonHttpMessageConverter

Raghuram
  • 51,854
  • 11
  • 110
  • 122
14

I'm doing in this way and it works .

HttpHeaders headers = createHttpHeaders(map);
public HttpHeaders createHttpHeaders(Map<String, String> map)
{   
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    for (Entry<String, String> entry : map.entrySet()) {
        headers.add(entry.getKey(),entry.getValue());
    }
    return headers;
}

// Pass headers here

 String requestJson = "{ // Construct your JSON here }";
logger.info("Request JSON ="+requestJson);
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
logger.info("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody());
logger.info("Response ="+response.getBody());

Hope this helps

Faraz
  • 6,025
  • 5
  • 31
  • 88
Yakhoob
  • 559
  • 1
  • 12
  • 32
10

If you are using Spring 3.0, an easy way to avoid the org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type exception, is to include the jackson jar files in your classpath, and use mvc:annotation-driven config element. As specified here.

I was pulling my hair out trying to figure out why the mvc-ajax app worked without any special config for the MappingJacksonHttpMessageConverter. If you read the article I linked above closely:

Underneath the covers, Spring MVC delegates to a HttpMessageConverter to perform the serialization. In this case, Spring MVC invokes a MappingJacksonHttpMessageConverter built on the Jackson JSON processor. This implementation is enabled automatically when you use the mvc:annotation-driven configuration element with Jackson present in your classpath.

Mike G
  • 4,713
  • 2
  • 28
  • 31
7

The "415 Unsupported Media Type" error is telling you that the server will not accept your POST request. Your request is absolutely fine, it's the server that's mis-configured.

MappingJacksonHttpMessageConverter will automatically set the request content-type header to application/json, and my guess is that your server is rejecting that. You haven't told us anything about your server setup, though, so I can't really advise you on that.

skaffman
  • 398,947
  • 96
  • 818
  • 769
6

Why work harder than you have to? postForEntity accepts a simple Map object as input. The following works fine for me while writing tests for a given REST endpoint in Spring. I believe it's the simplest possible way of making a JSON POST request in Spring:

@Test
public void shouldLoginSuccessfully() {
  // 'restTemplate' below has been @Autowired prior to this
  Map map = new HashMap<String, String>();
  map.put("username", "bob123");
  map.put("password", "myP@ssw0rd");
  ResponseEntity<Void> resp = restTemplate.postForEntity(
      "http://localhost:8000/login",
      map,
      Void.class);
  assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
}
eriegz
  • 335
  • 3
  • 10
  • This is really far down, but it worked for me. Not sure if there are any drawbacks, but it seems to be by far the simplest approach. – DavidS Mar 28 '22 at 02:26
4

I was getting this problem and I'm using Spring's RestTemplate on the client and Spring Web on the server. Both APIs have very poor error reporting, making them extremely difficult to develop with.

After many hours of trying all sorts of experiments I figured out that the issue was being caused by passing in a null reference for the POST body instead of the expected List. I presume that RestTemplate cannot determine the content-type from a null object, but doesn't complain about it. After adding the correct headers, I started getting a different server-side exception in Spring before entering my service method.

The fix was to pass in an empty List from the client instead of null. No headers are required since the default content-type is used for non-null objects.

Alex Worden
  • 3,374
  • 6
  • 34
  • 34
4

This code is working for me;

RestTemplate restTemplate = new RestTemplate();
Payment payment = new Payment("Aa4bhs");
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("payment", payment);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(map, headerObject);

Payment res = restTemplate.postForObject(url, httpEntity, Payment.class);
buræquete
  • 14,226
  • 4
  • 44
  • 89
Ganesh
  • 53
  • 1
  • 4
  • i am using a very similar approach and it did NOT work for me. for some reason my equivalent of your 'map' is not being converted to json or included as outbound body i.e., the target service does NOT see any payload. – abdel Jul 24 '18 at 14:41
  • yep, this solution as shown will post in `application/x-www-form-urlencoded` format. – rustyx Mar 05 '21 at 20:53
3

If you dont want to process response

private RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(serviceURL, request, Void.class);

If you need response to process

String result = restTemplate.postForObject(url, entity, String.class);
1

If you don't want to map the JSON by yourself, you can do it as follows:

RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
ResponseEntity<String> result = restTemplate.postForEntity(uri, yourObject, String.class);
moritz.vieli
  • 1,747
  • 1
  • 14
  • 17
1

I tried as following in spring boot:

ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {};
public Map<String, Object> processResponse(String urlendpoint)
{
    try{
    
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //reqobj
        JSONObject request = new JSONObject();
        request.put("username", name);
        //Or Hashmap 
        Map<String, Object> reqbody =  new HashMap<>();
        reqbody.put("username",username);
        Gson gson = new Gson();//mvn plugin to convert map to String
        HttpEntity<String> entity = new HttpEntity<>( gson.toJson(reqbody), headers);
        ResponseEntity<Map<String, Object>> response = resttemplate.exchange(urlendpoint, HttpMethod.POST, entity, typeRef);//example of post req with json as request payload
        if(Integer.parseInt(response.getStatusCode().toString()) == HttpURLConnection.HTTP_OK)
        {
            Map<String, Object>  responsedetails = response.getBody();
            System.out.println(responsedetails);//whole json response as map object
            return responsedetails;
        }
    } catch (Exception e) {
        // TODO: handle exception
        System.err.println(e);
    }
    return null;
}
Parameshwar
  • 856
  • 8
  • 16
0

For me error occurred with this setup:

AndroidAnnotations Spring Android RestTemplate Module and ...

GsonHttpMessageConverter

Android annotations has some problems with this converted to generate POST request without parameter. Simply parameter new Object() solved it for me.

Mateusz Jablonski
  • 1,039
  • 9
  • 11
0

You can make request as a JSON object

JSONObject request = new JSONObject();
request.put("name","abc"); 
ResponseEntity<JSONObject> response =restTemplate.postForEntity(append_url,request,JSONObject.class);                                                          `enter code here`
Asanka Sampath
  • 545
  • 4
  • 12