spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Unable to get 405 Method not supported status code, when requested method not found

Open satyam-bala opened this issue 1 year ago • 7 comments
trafficstars

Describe the bug Request to any other method(s), other then defined method, is not returning 405-"Method not supported" status code. Instead, returning 404-"Not found".

Sample Assume that we have a Route with "/users/" path and with GET operation only.
Request GET /users/ --> this is valid and returning valid response. -- no issues. Request POST /users/ --> this is return 404-"Not found", instead of 405-"Method not supported".

satyam-bala avatar Jul 23 '24 08:07 satyam-bala

@satyam-bala do you use the "Method Route Predicate" ? if you use it, I think the 404 means it can't match the right route.

GitHub-Yann avatar Jul 24 '24 00:07 GitHub-Yann

Using the default Predicate - RoutePredicateHandlerMapping.

More detailed logs: 2024-07-23T07:16:47.174Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] o.s.w.s.adapter.HttpWebHandlerAdapter : [ae8db915-7] HTTP DELETE "/api/v1/test?orgId=111" 2024-07-23T07:16:47.175Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] o.s.w.r.handler.SimpleUrlHandlerMapping : [ae8db915-7] Mapped to ResourceWebHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/]] 2024-07-23T07:16:47.177Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] o.s.w.r.resource.ResourceWebHandler : [ae8db915-7] Resource not found 2024-07-23T07:16:47.181Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] a.w.r.e.AbstractErrorWebExceptionHandler : [ae8db915-7] Resolved [NoResourceFoundException: 404 NOT_FOUND "No static resource api/v1/test."] for HTTP DELETE /api/v1/test 2024-07-23T07:16:47.182Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] o.s.http.codec.json.Jackson2JsonEncoder : [ae8db915-7] Encoding [{timestamp=Tue Jul 23 07:16:47 GMT 2024, path=/api/v1/test, status=404, error=Not Found, requestId=a (truncated)...] 2024-07-23T07:16:47.183Z DEBUG 1 --- [api-gateway] [or-http-epoll-6] o.s.w.s.adapter.HttpWebHandlerAdapter : [ae8db915-7] Completed 404 NOT_FOUND

satyam-bala avatar Jul 24 '24 01:07 satyam-bala

Please say the versions you are using and provide your configuration

spencergibb avatar Jul 24 '24 02:07 spencergibb

Version: org.springframework.cloud:spring-cloud-starter-gateway:jar:4.1.5

satyam-bala avatar Jul 24 '24 03:07 satyam-bala

Here is the Config:


server:
  port: 8080

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: test-self-read-api
          uri: http://test-api:8080/
          predicates:
            - Path=/api/v1/test/self
            - Method=GET
        - id: test-read-api
          uri: http://test-api:8080/
          predicates:
            - Path=/api/v1/test/{id}
            - Method=GET

satyam-bala avatar Jul 25 '24 06:07 satyam-bala

Hi Any update on this ?

Siddaramkj avatar Aug 23 '24 14:08 Siddaramkj

If you are using Circuit Breaker then check your fallback logic

github4dheeraj avatar Oct 06 '24 00:10 github4dheeraj

Gateway doesn't provide this functionality and I don't think we can since http method is an optional part of a predicate. You could create another route that matches the not allowed methods and use a SetStatus filter.

spencergibb avatar Jan 24 '25 00:01 spencergibb