Spring RestTemplate PostforObject: unsupported media type

I am trying to execute a RESTful web service, however, when I submit a request, an HttpClientErrorException occurs with the message "415 Unsupported Media Type".

This is the code to call the service:

MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(); request.add(val1, "xxxxx"); request.add(val2, "************"); request.add(val3, "xxx"); request.add("type", "AUTHENTICATE"); String response = restTemplate.postForObject(url, request, String.class); System.out.println(response.toString()); 

RestTemplate is connected from applicationContext.xml using FormHttpMessageConverter as its MessageConverter.

 <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <constructor-arg name="requestFactory" ref="httpClientFactory"/> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> </list> </property> </bean> <bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory"> <constructor-arg ref="httpClient"/> </bean> <bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams"> <property name="authenticationPreemptive" value="true"/> <property name="connectionManagerClass" value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/> </bean> <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"> <constructor-arg ref="httpClientParams"/> </bean> 

This is an exception:

 Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401) at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279) at ph.com.smart.drawbridge.commons.diameter.DiameterClient.post(DiameterClient.java:21) 

Any ideas?

+4
source share
2 answers

This problem occurs when your client sends a media type request that the server does not know how to handle. For example, your server-side method, which handles the url , can handle requests like application/json , but you sent text/xml . The server cannot process such a request. To fix this, find out what content-type (media type) the server is expecting, set the header to your restTemplate object, which sets the content-type:application/json (replace application/json with what the server expects). Also, make sure the request is sent in the correct format (if we are talking about json , you need to have jackson dependencies that can serialize your object into a json object).

+3
source

You may not need it now, but I will share it with everyone who faces this problem in the future.

As @Avi noted, this problem occurs when your server expects a different type of media compared to what you send to your request.

In my case, I submitted form data with MediaType.APPLICATION_JSON , while my server was expecting Mutipart form data. Adding the header below to my request solved my problem:

 HttpHeaders headers = getBasicAuthHttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); 
0
source

Source: https://habr.com/ru/post/1432199/


All Articles