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




Asked on June 20, 2016
I am working on spring client using RestTemplate.put(). I am getting following error. Any clue?

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:615) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:573) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:537) at org.springframework.web.client.RestTemplate.put(RestTemplate.java:387)


Replied on June 23, 2016
Issue is with Unsupported MediaType. You need to fix it.

Find the sample example of RestTemplate.put().
Server code.

@RequestMapping(value="/putdata/{id}/{name}", method = RequestMethod.PUT)
public void putData(@PathVariable(value = "id") String id,
           @PathVariable(value = "name") String name, @RequestBody Address address) {
System.out.println("Id:"+ id+ " Name:"+ name);
System.out.println("District:"+ address.getDistrict());
System.out.println("Village:"+ address.getVillage());

Client code.

public class PutDemo {
    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/spring-rest-1/data/putdata/{id}/{name}";
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "100");
        map.put("name", "Ram");
        Address address = new Address("Dhananjaypur", "Varanasi","UP");
        restTemplate.put(url, address, map);
    }

Find the URL
http://www.concretepage.com/spring/spring-mvc/spring-rest-client-resttemplate-consume-restful-web-service-example-xml-json#put


Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us