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( at org.springframework.web.client.RestTemplate.handleResponseError( at org.springframework.web.client.RestTemplate.doExecute( at org.springframework.web.client.RestTemplate.execute( at org.springframework.web.client.RestTemplate.put(

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

Write Answer

©2024 | Privacy Policy | Contact Us