Description
Matthew Pearson opened SPR-13944 and commented
When a request is made to a URL with a typo in it, Spring returns a 404 Not Found
for GET
and HEAD
, a 200 OK
for OPTIONS
, and a 405 Method Not Supported
for the other HTTP methods. I've tried to find specifications for what the response codes should be in this situation, but it's not clear to me. However, I think that returning a 404 in all cases would be most useful.
e.g. This Spring Boot application shows the different responses for requests to a mis-typed URL (greetings instead of greeting).
@RestController
@SpringBootApplication
public class Application {
private static Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
RestTemplate restTemplate = new RestTemplate();
String badUrl = "http://localhost:8080/greetings";
for (HttpMethod httpMethod : HttpMethod.values()) {
try {
restTemplate.execute(badUrl, httpMethod, null, null);
} catch (Exception e) {
log.error("Failed to " + httpMethod + " -- " + e.getMessage());
}
}
}
@RequestMapping("/greeting")
public String greeting() {
return "hello";
}
}
The logged output is:
Failed to GET -- 404 Not Found
Failed to HEAD -- 404 Not Found
Failed to POST -- 405 Method Not Allowed
Failed to PUT -- 405 Method Not Allowed
Failed to PATCH -- I/O error on PATCH request for "http://localhost:8080/greetings": Invalid HTTP method: PATCH; nested exception is java.net.ProtocolException: Invalid HTTP method: PATCH
Failed to DELETE -- 405 Method Not Allowed
OPTIONS request for "http://localhost:8080/greetings" resulted in 200 (OK)
Failed to TRACE -- 405 Method Not Allowed
Affects: 4.2.4
Reference URL: http://stackoverflow.com/questions/35387209/which-status-to-return-for-request-to-invalid-url-for-different-http-methods
Issue Links:
- Spring should have default support for OPTIONS, HEAD and Allow [SPR-13130] #17721 Spring should have default support for OPTIONS, HEAD and Allow
Referenced from: commits spring-attic/spring-framework-issues@cdba589, spring-attic/spring-framework-issues@a136060