32

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
EdwardLau
  • 1,139
  • 4
  • 20
  • 26
  • 1
    Do you see the log message in the welcome() method? – naikus Jul 26 '10 at 11:28
  • 3
    I'm still unclear about how to resolve this, there is no good answer here. No one explained why we can't do RequestMethod.POST. Can someone follow up? I don't understand any of the comments. – gene b. Oct 17 '16 at 20:38

9 Answers9

30

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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Tejas
  • 6,508
  • 1
  • 21
  • 25
  • this is by far the cleanest explanation of why my Spring API gives 'GET' not supported when i am using RequestMethod.DELETE in my Controller.! – mfaisalhyder Mar 10 '16 at 07:40
  • 3
    This does not solve the issue, it just explains basic knowledge. The "Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]" always appears when sending a form via POST. – FireFuro99 Jan 20 '22 at 15:01
16

Change

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

To

@RequestMapping(value = "/test", method = RequestMethod.GET)
Vikdor
  • 23,934
  • 10
  • 61
  • 84
Bik
  • 208
  • 3
  • 3
  • 13
    Why does `method=RequestMethod.POST` not work? The form method is POST and the action URL is /test, so I would have thought it will work. – Web User Feb 22 '12 at 05:03
  • 1
    @WebUser it is a myth, some says tomcat disables it by default, so they have to apply filter in web.xml but what if a dev is making an API with SPRING Boot framework, even today in 2016/March it is giving same error, but Using GET works clean and perfect in address bar.! Answer would be that one has to make whole panel then write Ajax calls to Delete/Put/Post to work.! – mfaisalhyder Mar 10 '16 at 07:50
4

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 })
Michel Fernandes
  • 1,187
  • 9
  • 8
  • Could this cause any security issue to add RequestMethod.GET to a RequestMethod.POST form (i.e. login form) ? – Ben Mar 10 '20 at 09:41
  • 2
    method = { RequestMethod.GET, RequestMethod.POST } is considered as security hotspot in SonarQube. Its betted to use GET or POST at a time. Avoid to use both together. – Abhishek Dec 03 '21 at 16:17
4

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.

Coffemanz
  • 863
  • 2
  • 8
  • 17
3

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

1

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));

0

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.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
0

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

0

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