Java 为什么选择SpringMVC请求方法';获取';不支持?
我正在尝试Java 为什么选择SpringMVC请求方法';获取';不支持?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试@RequestMapping(value=“/test”,method=RequestMethod.POST),但出现了错误 代码是 @Controller public class HelloWordController { private Logger logger = LoggerFactory.getLogger(HelloWordController.class); @RequestMapping(value = "/test", method = Request
@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";
}
}
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>
这里是Spring MVC DispatcherServlet
SpringMVC调度servlet
org.springframework.web.servlet.DispatcherServlet
SpringContext
上下文配置位置
类路径*:springmvc.xml
1.
SpringMVC调度servlet
/
而springmvc.xml是
jsp是
<form action="<%=request.getContextPath() %>/test" method="post">
<input type="submit" value="submit">
</form>
我输入提交波顿浏览器是错误的
HTTP状态405-请求方法“GET”
不支持类型状态报告
消息请求方法“GET”不可用
支持
说明指定的HTTP方法
不允许对请求的
资源(请求方法“GET”不是
支持)
改变
@RequestMapping(value = "/test", method = RequestMethod.POST)
到
method=POST
如果您将表单“发布”到url/test,则可以使用
如果在浏览器的地址栏中键入url并按enter键,则它始终是一个GET
请求,因此必须指定POST请求
谷歌搜索
httpget
和httppost
(还有一些类似于PUT DELETE的功能)。它们都有自己的含义。我通过在控制器中包含get和post请求来解决此错误:
method={RequestMethod.POST,RequestMethod.GET}我也有同样的问题。我把它改成了下面的样子,它成功了 爪哇:
@RequestMapping(value = "/test", method = RequestMethod.GET)
HTML代码:
<form action="<%=request.getContextPath() %>/test" method="GET">
<input type="submit" value="submit">
</form>
默认情况下,如果未在http方法使用的表单中指定http方法,则返回GET。要使用POST方法,您需要特别声明它
希望这有帮助 显然,有些POST请求看起来像是对服务器的“获取”(如Heroku…) 所以我使用了这个策略,它对我很有效:
@RequestMapping(value = "/salvar", method = { RequestMethod.GET, RequestMethod.POST })
如果您使用的是浏览器,它默认总是在get上工作,您可以使用postman工具,否则您可以将其更改为getmapping。希望这会起作用您是否在welcome()方法中看到日志消息?我仍然不清楚如何解决此问题,这里没有好的答案。没有人解释为什么我们不能做RequestMethod.POST。有人能跟进吗?我不理解任何评论。为什么
method=RequestMethod.POST
不起作用?表单方法是POST,操作URL是/test,所以我认为它会起作用。@WebUser这是一个神话,有些人说tomcat默认禁用它,所以他们必须在web.xml中应用过滤器,但是如果开发人员使用SPRING Boot framework制作API,即使在今天的2016/3月,它也会给出相同的错误,但是使用GET可以在地址栏中保持干净和完美。!答案是,我们必须制作整个面板,然后编写Ajax调用来删除/放置/发布工作。!这是迄今为止最清晰的解释,解释了当我在控制器中使用RequestMethod.DELETE时,为什么SpringAPI给出的“GET”不受支持。!这是否会导致将RequestMethod.GET添加到RequestMethod.POST表单(即登录表单)时出现任何安全问题?那么,为什么要特别声明“GET”?
<form action="<%=request.getContextPath() %>/test" method="GET">
<input type="submit" value="submit">
</form>
@RequestMapping(value = "/salvar", method = { RequestMethod.GET, RequestMethod.POST })