org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed




Asked on June 21, 2016
I am using RestTemplate.postForEntity() and getting folowing error. Any clue?

org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
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.postForEntity(RestTemplate.java:365)



Replied on June 23, 2016
You may be not passing correct arguments  in RestTemplate.postForEntity(). Find a sample example.

Server code.

@RequestMapping(value="/saveinfo/{id}/{name}", method = RequestMethod.POST)
public ResponseEntity<Person> saveInfo(@PathVariable(value = "id") Integer id,
          @PathVariable(value = "name") String name, @RequestBody Address address) {
Person person = new Person(id, name, address);
return new ResponseEntity<Person>(person, HttpStatus.CREATED);

Client code.

public class PostForEntityDemo {
    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/spring-rest-1/data/saveinfo/{id}/{name}";
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "111");
        map.put("name", "Shyam");
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
        ResponseEntity<Person> entity= restTemplate.postForEntity(url, address, Person.class, map);
        System.out.println(entity.getBody().getName());
        System.out.println(entity.getBody().getAddress().getVillage());
    }



Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us