Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Returning 405 instead of 404 for POST to unknown URL

Disclaimer: I'm new to Java, Spring, and Spring Boot.

I'd like to have Spring Boot return a 404 when trying to POST to a URL that doesn't exist. However, right now it's returning a 405, with an Allow header that only includes GET and HEAD. Is there a way to customize which HTTP methods are allowed so that I get a 404? I've tried implementing a custom ErrorController, but that doesn't seem to work.

To be clear: this is when I'm POSTing to a URL that shouldn't be matched by any of my defined endpoints, e.g http://example.com/some-bogus-thing

If any more information is needed to diagnose this, I'd be happy to provide it. Given my unfamiliarity with the platform, I'm not sure what's relevant.

like image 310
Brian Sullivan Avatar asked May 07 '15 13:05

Brian Sullivan


1 Answers

HTTP 405 (Method not found) is returned, when URL exists and you try to use an HTTP Method that is not allowed on that particular URL mapping.

if you invoke a POST on below .../test then it will return HTTP 405 and vice versa.

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

if there is no URL mapping for any of the HTTP methods, then it will return HTTP 404.

To know all the current mappings on that particular boot instance, just to go browser

http://localhost:8080/mappings
like image 64
K139 Avatar answered Sep 29 '22 01:09

K139