Why SpringMVC Request method 'GET' not supported?
Asked Answered
W

10

33

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

Wagner answered 26/7, 2010 at 9:28 Comment(2)
Do you see the log message in the welcome() method?Fighterbomber
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.Macbeth
S
16

Change

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

To

@RequestMapping(value = "/test", method = RequestMethod.GET)
Stimulate answered 9/11, 2010 at 19:32 Comment(2)
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.Watercolor
@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.!Cotemporary
W
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.

Wellfounded answered 4/9, 2012 at 11:56 Comment(2)
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.!Cotemporary
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.Safier
S
16

Change

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

To

@RequestMapping(value = "/test", method = RequestMethod.GET)
Stimulate answered 9/11, 2010 at 19:32 Comment(2)
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.Watercolor
@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.!Cotemporary
B
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 })
Brambly answered 24/5, 2019 at 12:28 Comment(2)
Could this cause any security issue to add RequestMethod.GET to a RequestMethod.POST form (i.e. login form) ?Cultch
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.Manutius
H
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.

Heulandite answered 17/1, 2022 at 17:48 Comment(0)
M
3

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

Morganne answered 22/1, 2018 at 14:10 Comment(0)
H
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));

Harry answered 30/12, 2022 at 2:52 Comment(0)
W
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.

Willingham answered 17/5, 2018 at 9:10 Comment(1)
so why do you specifically state "GET"?Salty
S
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

Slyke answered 30/1, 2020 at 5:18 Comment(0)
R
0

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

Rostov answered 13/4, 2022 at 7:24 Comment(0)
P
0

For me dosn't work I put solution abowe, You have to implement whole path like it's on pictures. Good luck

As well don't forget use:

@Service
@Transactional
public class RoomService {

enter image description here

enter image description here

    @RequestMapping(value = "/{roomId}/status/{status}", method = RequestMethod.GET)
    public ResponseEntity<String> changeRoomStatus(
            @PathVariable int roomId,
            @PathVariable RoomStatus status) {

        try {
            roomService.changeRoomStatus(roomId, status);
            return ResponseEntity.ok("Room status changed successfully.");
        } catch (RoomNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Room not found.");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Error changing room status: " + e.getMessage());
        }
    }
Primine answered 3/12, 2023 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.