我正在使用Postman,在使用PUT方法更新信息时遇到了麻烦。
我有以下控制器:
@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);
}
}
个字符
下面是我更新 * 人员 * 信息的过程:
@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);
}
型
这是我从 Postman 那里打来的电话。我尝试了POST,GET和DELETE,它们都工作正常,但当我使用PUT更新数据时,我得到了这个错误:
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:已解决[org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'PUT']
2023-07-09T16:51:04.655+02:00 DEBUG 35932 - [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet:已完成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:Map到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:使用'application/json',给定[/]并支持[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:正在写入[{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:退出“ERROR”调度,状态405
x1c 0d1x的数据
这是我的依赖:
<?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>
型
有人知道我的问题是什么吗?
我试图仔细检查网址以及搜索几个网站的解决方案,但这些都没有为我工作
1条答案
按热度按时间lrl1mhuk1#
像这样更新您的控制器
字符串