Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SpringMVC Request method 'GET' not supported?

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

like image 792
EdwardLau Avatar asked Jul 26 '10 09:07

EdwardLau


People also ask

What is request method GET not supported?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.

How do I fix 405 method not allowed in spring boot?

As the name here suggests, the reason for this error is sending the request with a non-supported method. As we can expect, we can solve this by defining an explicit mapping for PUT in the existing method mapping: @RequestMapping( value = "/employees", produces = "application/json", method = {RequestMethod.

What is Springmvc model?

In Spring MVC, the model works a container that contains the data of the application. Here, a data can be in any form such as objects, strings, information from the database, etc. It is required to place the Model interface in the controller part of the application.

How does Spring MVC handle request?

As seen in earlier section, the web container directs all MVC request to the Spring DispatcherServlet. The Spring Front controller will intercept the request and will find the appropriate handler based on the handler mapping (configured in Spring configuration files or annotation).


2 Answers

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.

like image 129
Tejas Avatar answered Oct 03 '22 17:10

Tejas


Change

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

To

@RequestMapping(value = "/test", method = RequestMethod.GET) 
like image 37
Bik Avatar answered Oct 03 '22 19:10

Bik