开发者

Why SpringMVC Request method 'GET' not supported?

开发者 https://www.devze.com 2023-01-09 01:24 出处:网络
I trying @RequestMapping(value = \"/test\", method = RequestMethod.POST) but is error Code is @Controller

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

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

<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

index.jsp is

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

I input sub开发者_JAVA技巧mit 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).


method = POST will work if you 'post' a form to the 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.

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


Change

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

To

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


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

So I use this strategy and it works for me:

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


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


For me the problem was that I forgot to add a protocol to baseUrl in my postman request. After I've added "https://", it worked like a charm.


Are you working with Angular? I had the same problem and it was because I was using subscribe and they changed their implementation (This deprecation was introduced in RxJS 6.4.) It worked for me using it like this:

import { of } from 'rxjs';

// recommended of([1,2,3]).subscribe((v) => console.info(v));


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

Java :

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

HTML code:

  <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.

Hope this helps.


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


I solved this error by including json data into postman body part and then hit the postmapping url

0

精彩评论

暂无评论...
验证码 换一张
取 消