不支持 Springboot 请求方法“POST" [英] Springboot Request method 'POST' not supported

查看:58
本文介绍了不支持 Springboot 请求方法“POST"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习用于 REST API 开发的 Java Spring Boot.使用下面的代码,GET 方法工作正常,但 POST 没有.

I have just started learning Java Spring Boot for REST API development. With below code, GET method is working fine but POST doesn't.

@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

错误,

{
    "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 版本,

Java and springboot version for maven,

<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命令运行项目解决了问题.

Finally, solved the problem by running the project using maven command.

不是使用IDE的运行按钮运行项目,而是在IDE中进入终端并执行命令

Instead of running project using IDE's run button, just went to the terminal in IDE and executed command

mvn spring-boot:run

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

As it is a REST implementation, now I can access these,

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

像魅力一样工作!

这篇关于不支持 Springboot 请求方法“POST"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆