I am using Postman and having trouble with using PUT method to update information.
I have following controller:
@RequestMapping("api/v1/person")
@RestController
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
@PutMapping
public void updatePerson(@PathVariable("id") UUID id, @RequestBody Person personToUpdate){
personService.updatePerson(id, personToUpdate);
}
}
public int updatePerson(UUID id, Person newPerson){
return personDao.updatePersonById(id, newPerson);
}
Here is procedure that I update the Person information:
@Override
public int updatePersonById(UUID id, Person update) {
return selectPersonById(id)
.map(person -> {
int indexOfPersonToUpdate = DB.indexOf(person);
if (indexOfPersonToUpdate >= 0){
DB.set(indexOfPersonToUpdate,new Person(id, update.getName()));
return 1;
}
return 0;
})
.orElse(0);
}
Here is my calls from the PostMan. I tried POST, GET and DELETE and they all work fine, but when I used PUT to update data then I got this error:
2023-07-09T16:51:04.651+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : PUT "/api/v1/person/ef462667-fbc2-4587-8099-73475e68f33c", parameters={}
2023-07-09T16:51:04.655+02:00 WARN 35932 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' is not supported]
2023-07-09T16:51:04.655+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 405 METHOD_NOT_ALLOWED
2023-07-09T16:51:04.659+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for PUT "/error", parameters={}
2023-07-09T16:51:04.660+02:00 DEBUG 35932 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-07-09T16:51:04.664+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [/] and supported [application/json, application/*+json]
2023-07-09T16:51:04.665+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sun Jul 09 16:51:04 CEST 2023, status=405, error=Method Not Allowed, path=/api/v1/person/ (truncated)...]
2023-07-09T16:51:04.673+02:00 DEBUG 35932 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 405
And here is my dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Does anyone know what my problem is?
I tried to double check the URL as well as search a few sites for a solution but those didn't work for me
Update your controller like this
@PutMapping("/{id}")
public void updatePerson(@PathVariable("id") UUID id, @RequestBody Person personToUpdate){
personService.updatePerson(id, personToUpdate);
}