Repository navigation
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
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
Activity
spring-projects-issues commentedon Feb 14, 2016
Juergen Hoeller commented
This might have been addressed in 4.3 snapshots already, along with our recently revised OPTIONS handling.
Rossen Stoyanchev, in any case, let's make sure to define consistent behavior here as of 4.3.
Juergen
spring-projects-issues commentedon Feb 14, 2016
Matthew Pearson commented
See also spring-framework-issues pull request.
spring-projects-issues commentedon Feb 14, 2016
Matthew Pearson commented
I've created a spring-framework pull request with a potential fix. Although, looking at the file history, the behaviour of OPTIONS has only recently been implemented in response to default support for OPTIONS. So maybe OPTIONS should stay as it is.
spring-projects-issues commentedon Feb 17, 2016
Rossen Stoyanchev commented
I've had a closer look. First the ProtocolException for PATCH is due to the use of the default SimpleClientHttpRequestFactory but switching to HttpComponentsClientHttpRequestFactory removes that issue. That said with just Spring Framework 4.3 RC1 for all HTTP methods the status is 404. The real issue comes when static resource handling is also configured. This is the case in Spring Boot where it's mapped to "/". In effect ResourceHttpRequestHandler seems to assume that if invoked it must be a resource request, which would be true if using some prefix-based mapping but not so with a "/" mapping. In short the proposed fix makes sense and I've incorporated it.