gpt4 book ai didi

java - Springboot请求方法 'POST'不支持

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:01 26 4
gpt4 key购买 nike

我刚刚开始学习用于 REST API 开发的 Java Spring Boot。使用下面的代码,GET 方法可以正常工作,但 POST 却不能。

@RestController
@RequestMapping("/api/users")
public class UsersController {

@Autowired
private UserRepository userRepository;

@RequestMapping(method = RequestMethod.GET)
public List<User> getAll() {
return userRepository.findAll();
}

@RequestMapping(method = RequestMethod.POST)
public String saveUser() {
return "Saved";
}

}

使用 Content-Type 作为 application/json 在 Postman 应用程序中测试 POST 方法

错误,

{
"timestamp": 1497116929266,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/api/users/"
}

在日志中我可以看到,

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/users],methods=[POST]}" onto public java.lang.String com.betasquirrel.controller.UsersController.saveUser()
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/users],methods=[GET]}" onto public java.util.List<com.betasquirrel.model.User> com.betasquirrel.controller.UsersController.getAll()
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

maven 的 Java 和 springboot 版本,

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

最佳答案

最后通过maven命令运行项目解决了问题。

无需使用 IDE 的运行按钮来运行项目,只需转到 IDE 中的终端并执行命令

mvn spring-boot:run

由于它是一个 REST 实现,现在我可以访问这些,

GET /api/users/ or /api/users
POST /api/users/ or /api/users

像魅力一样工作!

关于java - Springboot请求方法 'POST'不支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com