【问题标题】:Request method 'POST' not supported. 500 Internal server ERRor Spring-boot不支持请求方法“POST”。 500 Internal server ERRor Spring-boot
【发布时间】:2017-02-27 06:41:55
【问题描述】:

我正在为我的网络服务编写一个新的 API。我已经尝试了与此相关的所有其他 stackoverflow 链接。但问题仍然存在。

问题是: 当我尝试使用以下方式访问我的 spring-boot 应用程序时:

curl -v -XPOST http://localhost:9081/postpayment/v1/invoice/djhbjsd/send

我得到以下回复:

 Trying ::1...
 Connected to localhost (::1) port 9081 (#0)
 POST /postpayment/v1/invoice/djhbjsd/send HTTP/1.1
 Host: localhost:9081
 User-Agent: curl/7.43.0
 Accept: */*

 HTTP/1.1 500 Internal Server Error
 Server: Apache-Coyote/1.1
 Content-Type: application/json;charset=UTF-8
 Transfer-Encoding: chunked
 Date: Mon, 27 Feb 2017 06:21:43 GMT
 Connection: close

 Closing connection 0
 {"timestamp":1488176503569,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/postpayment/v1/invoice/djhbjsd/send"}

我的控制器是:

@RestController
public class PostPaymentApiController {


     @ResponseBody
     @RequestMapping(value = "/postpayment/v1/invoice/{invoiceGUID}/send", method = RequestMethod.POST)
     public ResponseEntity<String> sendPaymentConfirmation(
        @PathVariable("invoiceGUID") String invoiceGUID) throws Exception {

        try {
           //logic

        } catch (Exception e) {

            LOGGER.error(invoiceGUID+" Error in sending payment confirmation",e);
            return new ResponseEntity<String>(invoiceGUID+" "+e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<String>(invoiceGUID+" payment confirmation sent successfully",HttpStatus.OK);
     }

}

日志错误:

: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request method 'POST' not supported] with root cause
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
at org.springframework.web.servlet.support.WebContentGenerator.checkRequest(WebContentGenerator.java:301) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:229) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcChildContextConfiguration$CompositeHandlerAdapter.handle(EndpointWebMvcChildContextConfiguration.java:266) ~[spring-boot-actuator-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]

还有一件事。我在管理端口上得到以下响应。当我点击服务器端口时,我得到 404。我的 application.yml 包含:

    server.port: 9080
    management.port: 9081 

【问题讨论】:

  • 你为什么将@Configuration 添加到你的RestController?
  • @ResponseBody in @RestController 是多余的顺便说一句
  • 从控制器中删除了@Configuration。
  • 您确定要使用Spring Boot Actuator,因为它应该用于监控您的应用程序
  • 我们将它用于健康检查和其他一些指标。

标签: spring spring-mvc spring-boot spring-web


【解决方案1】:

您的休息控制器包括@Configuration 和处理程序方法包括@ResponseBody 这在@RestController 中是不需要的,因此请从您的控制器中删除@Configuration 和@ResponseBody

【讨论】:

  • 你的项目名称是什么,或者你可以上传你的项目的文件夹结构
  • 这个 repo 不是公开的。你看不到 repo.btw 项目名称是“postpayment”
  • 请在您的其他客户端(邮递员)中尝试此操作,我认为这将在 postpayment/postpayment/v1/invoice/{invoiceGUID}/send 或将处理程序方法更新为 @RequestMapping(value="/v1/ invoice/{invoiceGUID}/send").... 希望它有效
  • 刚试过。不工作。但我很想知道,为什么你在 uri 之前添加了后付款。
  • 项目名称是 Spring MVC 应用程序的根 URL
猜你喜欢
  • 1970-01-01
  • 2018-06-03
  • 2020-03-31
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多