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
Why SpringMVC Request method 'GET' not supported?
提问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 = POST
will 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 GET
request, so you had to specify POST request.
如果你在浏览器的地址栏中输入一个 url 并按回车键,它总是一个GET
请求,所以你必须指定 POST 请求。
Google for HTTP GET
and HTTP POST
(there are several others like PUT DELETE). They all have their own meaning.
谷歌搜索HTTP GET
和HTTP 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。希望这会起作用