Java 为什么不支持 SpringMVC 请求方法“GET”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3333611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:19:16  来源:igfitidea点击:

Why SpringMVC Request method 'GET' not supported?

javaspringspring-mvc

提问by EdwardLau

I trying @RequestMapping(value = "/test", method = RequestMethod.POST)but is error

我正在尝试@RequestMapping(value = "/test", method = RequestMethod.POST)但错误

Code is

代码是

 @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 is

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>

and springmvc.xml is

和 springmvc.xml 是

index.jsp is

index.jsp 是

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

I input submit botton brower is error

我输入提交按钮浏览器是错误的

HTTP Status 405 - Request method 'GET' not supported type Status report

message Request method 'GET' not supported

description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).

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

不支持消息请求方法“GET”

描述 请求的资源不允许指定的 HTTP 方法(不支持请求方法“GET”)。

采纳答案by Bik

Change

改变

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

To

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

回答by Tejas

method = POSTwill work if you 'post' a form to the url /test.

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

if you type a url in address bar of a browser and hit enter, it's always a GETrequest, so you had to specify POST request.

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

Google for HTTP GETand HTTP POST(there are several others like PUT DELETE). They all have their own meaning.

谷歌搜索HTTP GETHTTP POST(还有其他几个像 PUT DELETE)。他们都有自己的意义。

回答by Gavin James Beere

I solved this error by including a get and post request in my controller: method={RequestMethod.POST, RequestMethod.GET}

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

回答by Dulith De Costa

I also had the same issue. I changed it to the following and it worked.

我也有同样的问题。我将其更改为以下内容并且它起作用了。

Java :

爪哇:

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

HTML code:

HTML代码:

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

By default if you do not specify http method in a form it uses GET. To use POST method you need specifically state it.

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

Hope this helps.

希望这可以帮助。

回答by Michel Fernandes

Apparently some POST requests looks like a "GET" to the server (like Heroku...)

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

So I use this strategy and it works for me:

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

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

回答by Ajith Arumugam Arunachalam

if You are using browser it default always works on get, u can work with postman tool,otherwise u can change it to getmapping.hope this will works

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