Skip to content

Mis-typed URL should give 404 not 405 [SPR-13944] #18516

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits spring-attic/spring-framework-issues@cdba589, spring-attic/spring-framework-issues@a136060

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions