Spring MVC RestController POST请求错误500

时间:2018-03-22 10:28:42

标签: http spring-mvc tomcat7 spring-restcontroller

我正在尝试使用Spring MVC构建一个REST控制器,以便从Android应用程序接收请求。我有一个GET请求正常工作。但是,当我尝试执行POST请求时,我遇到了一些错误。 如果我从表单执行POST请求,一切正常,但是,如果我从Android应用程序或其他工具(例如Postman)发送请求,我会收到内部服务器错误500.

我收到以下错误:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:207)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:378)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:317)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:62)
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:351)
org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1188)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:964)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我不认为这是代码问题,但我将链接控制器的简化版本和实际执行POST的表单:

控制器:

@RestController
public class HeartRateController {

@PostMapping(value = ("/saveheartrates"))
public ResponseEntity<String> saveHeartRates() {
    return new ResponseEntity<String>("Worked", HttpStatus.OK);
}

}

Html表格:

<form class="form-narrow form-horizontal" method="post" th:action="@{/saveheartrates}" th:fragment="signupForm">
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button type="submit" class="btn btn-default">Try post</button>
        </div>
    </div>

我正在使用tomcat7来运行该应用程序。

0 个答案:

没有答案