title: Which status to return for request to invalid URL for different http methods?
body: <p>When a REST application receives a request for a non-existent resource, should it always return a <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5" rel="nofollow noreferrer"><code>404 Not Found</code></a>?</p>
  <p>Should it return a different status for any of the <a href="https://www.rfc-editor.org/rfc/rfc7231#section-4.1" rel="nofollow noreferrer">HTTP methods</a> <code>GET</code>, <code>HEAD</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code>, <code>OPTIONS</code> or <code>TRACE</code>?</p>
  <p>Spring returns a <code>404</code> for <code>GET</code> and <code>HEAD</code>, a <code>200 OK</code> for <code>OPTIONS</code>, and a <code>405 Method Not Supported</code> for the others. Is that wrong?</p>
  <p>e.g. This Spring Boot application shows the different responses for requests to a mis-typed URL (<em>greetings</em> instead of <em>greeting</em>).</p>
  <pre><code>@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 = &quot;http://localhost:8080/greetings&quot;;
          for (HttpMethod httpMethod : HttpMethod.values()) {
              try {
                  restTemplate.execute(badUrl, httpMethod, null, null);
              } catch (Exception e) {
                  log.error(&quot;Failed to &quot; + httpMethod + &quot; -- &quot; + e.getMessage());
              }
          }
      }
  
      @RequestMapping(&quot;/greeting&quot;)
      public String greeting() {
          return &quot;hello&quot;;
      }
  }
  </code></pre>
  <p>The logged output is:</p>
  <blockquote>
  <p>Failed to GET -- 404 Not Found</p>
  </blockquote>
  <blockquote>
  <p>Failed to HEAD -- 404 Not Found</p>
  </blockquote>
  <blockquote>
  <p>Failed to POST -- 405 Method Not Allowed</p>
  </blockquote>
  <blockquote>
  <p>Failed to PUT -- 405 Method Not Allowed</p>
  </blockquote>
  <blockquote>
  <p>Failed to PATCH -- I/O error on PATCH request for &quot;http://localhost:8080/greetings&quot;: Invalid HTTP method: PATCH; nested exception is java.net.ProtocolException: Invalid HTTP method: PATCH</p>
  </blockquote>
  <blockquote>
  <p>Failed to DELETE -- 405 Method Not Allowed</p>
  </blockquote>
  <blockquote>
  <p>OPTIONS request for &quot;http://localhost:8080/greetings&quot; resulted in 200 (OK)</p>
  </blockquote>
  <blockquote>
  <p>Failed to TRACE -- 405 Method Not Allowed</p>
  </blockquote>