【问题标题】:Why SpringMVC Request method 'GET' not supported?为什么不支持 SpringMVC 请求方法“GET”?
【发布时间】:2011-03-21 00:03:44
【问题描述】:

我尝试@RequestMapping(value = "/test", method = RequestMethod.POST) 但出错了

代码是

 @Controller
 public class HelloWordController {
 private Logger logger = LoggerFactory.getLogger(HelloWordController.class);

 @RequestMapping(value = "/test", method = RequestMethod.POST)
 public String welcome() {
  logger.info("Spring params is welcome");
  return "/WEB-INF/jsp/welcome";
 }

}

web.xml 是

<servlet>
<description>This is Spring MVC DispatcherServlet</description>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <description>SpringContext</description>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

  <servlet-mapping>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>

而springmvc.xml是

index.jsp 是

<form action="<%=request.getContextPath() %>/test" method="post">
<input type="submit" value="submit"> 
</form>

我输入提交按钮浏览器出错

HTTP 状态 405 - 请求方法“GET” 不支持类型状态报告

消息请求方法'GET'不 支持

description 指定的HTTP方法 不允许请求的 资源(请求方法 'GET' 不是 支持)。

【问题讨论】:

  • 在welcome()方法中看到日志信息了吗?
  • 我还不清楚如何解决这个问题,这里没有好的答案。没有人解释为什么我们不能做 RequestMethod.POST。有人可以跟进吗?我不懂任何cmets。

标签: java spring spring-mvc


【解决方案1】:

改变

@RequestMapping(value = "/test", method = RequestMethod.POST)

@RequestMapping(value = "/test", method = RequestMethod.GET)

【讨论】:

  • 为什么method=RequestMethod.POST 不起作用?表单方法是 POST,操作 URL 是 /test,所以我认为它会起作用。
  • @WebUser 这是一个神话,有人说tomcat默认禁用它,所以他们必须在web.xml中应用过滤器但是如果开发人员正在使用SPRING Boot框架制作API怎么办,即使在今天2016/3 月它给出了同样的错误,但使用 GET 在地址栏中工作干净和完美。!答案是必须制作整个面板,然后编写 Ajax 调用来删除/放置/发布才能工作。!
【解决方案2】:

method = POST 将在您将表单“发布”到 url /test 时起作用。

如果你在浏览器的地址栏中输入一个url并回车,它总是一个GET请求,所以你必须指定POST请求。

Google for HTTP GETHTTP POST(还有其他几个,例如 PUT DELETE)。它们都有各自的含义。

【讨论】:

  • 这是迄今为止我在 Controller 中使用 RequestMethod.DELETE 时为什么我的 Spring API 不支持“GET”的最清晰的解释。!
  • 这并不能解决问题,它只是解释了基础知识。通过 POST 发送表单时,总是出现“已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]”。
【解决方案3】:

我通过在我的控制器中包含一个 get 和 post 请求解决了这个错误: 方法={RequestMethod.POST, RequestMethod.GET}

【讨论】:

    【解决方案4】:

    我也有同样的问题。我将其更改为以下内容并且有效。

    Java:

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    

    HTML 代码:

      <form action="<%=request.getContextPath() %>/test" method="GET">
        <input type="submit" value="submit"> 
        </form>
    

    默认情况下,如果您未在表单中指定 http 方法,则它使用 GET。要使用 POST 方法,您需要特别说明它。

    希望这会有所帮助。

    【讨论】:

    • 那你为什么要特别注明“GET”?
    【解决方案5】:

    显然,一些 POST 请求看起来像是对服务器的“GET”(如 Heroku...)

    所以我使用了这个策略,它对我有用:

    @RequestMapping(value = "/salvar", method = { RequestMethod.GET, RequestMethod.POST })
    

    【讨论】:

    • 这是否会导致将 RequestMethod.GET 添加到 RequestMethod.POST 表单(即登录表单)中的任何安全问题?
    • method = { RequestMethod.GET, RequestMethod.POST } 在 SonarQube 中被视为安全热点。它打赌一次使用 GET 或 POST。避免同时使用两者。
    【解决方案6】:

    如果您使用的是浏览器,它默认始终适用于 get,您可以使用邮递员工具,否则您可以将其更改为 getmapping。希望这会有效

    【讨论】:

      【解决方案7】:

      对我来说,问题是我忘记在我的邮递员请求中向 baseUrl 添加协议。在我添加“https://”之后,它就像一个魅力。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-30
        • 2015-07-10
        • 1970-01-01
        • 2020-08-14
        • 1970-01-01
        • 2013-03-03
        • 2020-08-31
        • 2021-08-01
        相关资源
        最近更新 更多