Request method ‘GET’ not supported – Http Status 405

The spring boot exception Request method ‘GET’ not supported exists when the request method is not configured as ‘GET’ in one of the rest controller methods and is requested using the HTTP GET method. The request method is configured in annotation @RequestMapping. In spring boot, all HTTP request methods are available in the RequestMethod enum. If a rest controller method is not configured as GET, the exception Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported] will be thrown.

In spring boot, if the requested url is not configured as ‘GET’ call in rest controller, PageNotFound : Request method ‘GET’ not supported exception will be thrown from the application. It also throws org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported in spring boot as the method is not available in spring boot application.



Exception

2019-10-04 17:39:32.012  WARN 2448 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
 2019-10-04 17:39:32.034  WARN 2448 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Dec 30 20:28:12 IST 2020
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported


HTTP Request Methods Supported in Spring Boot

The following http request methods are supported in spring boot application. The requested HTTP methods are available in the RequestMethod Enum class. Request methods are used in the @RequestMapping annotation.

public enum RequestMethod {
	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}


How to reproduce this issue

In the spring boot rest controller class, remove one of the url in a method. If you access the url from the browser or postman, This warning will be shown in the log.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value="/welcome/", method=RequestMethod.POST)
	public String welcomepage() {
		return "Welcome to Yawin Tutor\n";
	}
}

Output

$ curl -X GET  http://localhost:8080/welcome
{"timestamp":"2020-12-30T15:19:18.265+0000","status":404,"error":"Not Found","message":"No message available","path":"/welcome"}


Root Cause

If the http request type of curl or postman or browser is ‘GET’ and the corresponding method in the rest controller is not listening in HTTP GET method, The request call could not find the rest controller method. The exception will be thrown for non availability of the url with the right HTTP method in rest controller class.



Scenario 1

In the spring boot application, the request url called from curl command or postman or browser may be wrong or mis-spelled. In this case, the requested url is not available in the rest controller. There fore the exception will be thrown. Verify the request url with the corresponding rest controller method and correct any spelling issues.

$ curl -X GET  http://localhost:8080/welcome1/
{"timestamp":"2020-12-30T15:32:27.383+0000","status":404,"error":"Not Found","message":"No message available","path":"/welcome1/"}

Solution

$ curl -X GET http://localhost:8080/welcome/
Welcome to Yawin Tutor


Scenario 2

The request method in the rest controller can be improperly configured. The request method is configured in the @RequestMapping annotation. The default request method in the @RequestMapping annotation is ‘GET.’ The request method may be configured in a way other than the ‘GET’ method.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value="/welcome/", method=RequestMethod.POST)
	public String welcomepage() {
		return "Welcome to Yawin Tutor\n";
	}
}

Output

$ curl -X GET  http://localhost:8080/welcome/
{"timestamp":"2020-12-30T15:19:18.265+0000","status":404,"error":"Not Found","message":"No message available","path":"/welcome/"}

In the above example, the method is configured with POST and request url has GET method. Hence, the exception HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported will be thrown.



Scenario 3

The rest controller method is configured with the wrong url in the @RequestMapping annotation. If the url is incorrect, the requested url would not be available in the rest controller class. The exception would then be thrown.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value="/welcome1/", method=RequestMethod.GET)
	public String welcomepage() {
		return "Welcome to Yawin Tutor\n";
	}
}

Output

$ curl -X GET  http://localhost:8080/welcome/
{"timestamp":"2020-12-30T15:19:18.265+0000","status":404,"error":"Not Found","message":"No message available","path":"/welcome/"}



Related Articles

3 Comments

  • satish , January 27, 2021 @ 1:28 PM

    isn’t sol1 & sol3 same
    and instead of solution, it will be better if you replace it with scenario

    • Yawin Tutor , January 27, 2021 @ 3:39 PM

      Yes, solution1 and solution3 are similar but your focus is different.
      In solution1, invalid url is configured. In solution3, invalid method configuration.
      It depends on where you want correct in your code.

      Thanks

    • Yawin Tutor , January 27, 2021 @ 3:45 PM

      Hi Satish,

      Thanks for your suggestions, change from solution to scenario.

Leave a Reply

Your email address will not be published. Required fields are marked *