简体   繁体   English

spring boot api 不时返回 404 Not Found

[英]spring boot api return 404 Not Found time to time

It is normal to use most of the time, and 404 appears occasionally.大部分时间使用是正常的,偶尔会出现404。 I don't know how to locate the problem.我不知道如何定位问题。

controller file:控制器文件:

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @GetMapping("info")
    public Result info(@RequestParam("token") String token) {
        Map<String, Object> stringObjectMap = authService.getInfo(token);

        return ResultGenerator.success(stringObjectMap);
    }
}

GET: localhost:9000/v1/auth/info?token=gNGLJLLZsluDsIQw This ERROR MESSAGE is displaying time to time: GET: localhost:9000/v1/auth/info?token=gNGLJLLZsluDsIQw 此错误消息不时显示:

{
    "timestamp": "2021-06-29T06:46:35.477+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/auth/info"
}

version info:版本信息:

  • spring boot: 2.2.6.RELEASE弹簧靴:2.2.6.RELEASE
  • spring cloud: Hoxton.SR1春云:Hoxton.SR1

Append:附加:

spring cloud gateway yml config: spring 云网关 yml 配置:

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin
      globalcors:
        cors-configurations:
          "[/**]":
            allowCredentials: true
            allowedOrigins: "*"
            allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Content-Length, TOKEN, Authorization"
            allowedMethods: "GET, POST, PATCH, PUT, DELETE, OPTIONS"
            maxAge: 3628800
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/v1/auth/**
          filters:
            - StripPrefix=1
        - id: bus-service
          uri: lb://bus-service
          predicates:
            - Path=/v1/**
          filters:
            - StripPrefix=1

Solution to be verified待验证解决方案

I used the zipkin, found the route /v1/auth/info matched to bus-service(/v1/**) , so return 404 not found.我使用了 zipkin,发现路由 /v1/auth/info 与bus-service(/v1/**)匹配,因此返回 404 not found。

From this to the conclusion: The writing order of the route does not guarantee its matching priority.由此得出结论:路由的写入顺序并不能保证其匹配优先级。 So must add order configuration.所以必须添加order配置。

Try removing @RequestMapping("/auth") and replacing @GetMapping("info") to look like below:尝试删除@RequestMapping("/auth")并替换@GetMapping("info")如下所示:

@GetMapping("auth/info")
public Result info(@RequestParam("token") String token) {
    Map<String, Object> stringObjectMap = authService.getInfo(token);

Please add / before info like this请在这样的信息之前添加 /

 @GetMapping("/info")
    public Result info(@RequestParam("token") String token) {
        Map<String, Object> stringObjectMap = authService.getInfo(token);

        return ResultGenerator.success(stringObjectMap);
    }

Since you are using @RequestParam, you need to pass value like this由于您使用的是@RequestParam,您需要像这样传递值

localhost:9000/auth/info?token="token_value"本地主机:9000/auth/info?token="token_value"

Please check this:请检查这个:

If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute.如果 url 具有 lb 方案(即 lb://myservice),它将使用 Spring Cloud LoadBalancerClient 将名称(上例中的 myservice)解析为实际主机和端口,并替换同一属性中的 URI。

By default when a service instance cannot be found in the LoadBalancer a 503 will be returned.默认情况下,当在 LoadBalancer 中找不到服务实例时,将返回 503。 You can configure the Gateway to return a 404 by setting spring.cloud.gateway.loadbalancer.use404=true.您可以通过设置 spring.cloud.gateway.loadbalancer.use404=true 将网关配置为返回 404

You can also try to specify an order in routes list:您也可以尝试在路由列表中指定一个顺序

routes:
  - id: auth-service
    uri: lb://auth-service
    order: 0
    predicates:
      - Path=/v1/auth/**
    filters:
      - StripPrefix=1
  - id: bus-service
    uri: lb://bus-service
    order: 1
    predicates:
      - Path=/v1/**
    filters:
      - StripPrefix=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM