【问题标题】:spring boot api return 404 Not Found time to timespring boot api不时返回404 Not Found
【发布时间】:2021-09-11 09:26:29
【问题描述】:

大部分时间使用正常,偶尔出现404。不知道怎么定位问题。

控制器文件:

@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 此错误消息不时显示:

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

版本信息:

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

追加:

spring cloud gateway 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

解决方案待验证

我使用了zipkin,发现路由/v1/auth/info匹配bus-service(/v1/**),所以返回404 not found。

由此得出结论:路由的写入顺序并不能保证其匹配优先级。所以必须添加order配置。

【问题讨论】:

  • 我想问题不在于这个控制器。请出示你的spring cloud代码
  • @DmitriyPankratov 我添加了网关的 yml 配置

标签: java spring spring-boot http-status-code-404


【解决方案1】:

尝试删除 @RequestMapping("/auth") 并替换 @GetMapping("info") 如下所示:

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

【讨论】:

    【解决方案2】:

    请在这样的信息之前添加/

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

    由于您使用的是@RequestParam,因此您需要像这样传递值

    localhost:9000/auth/info?token="token_value"

    【讨论】:

    • 对不起,我忘了补充这个问题。我更新了这个问题
    【解决方案3】:

    请检查:

    如果 url 有 lb 方案(即 lb://myservice),它将使用 Spring Cloud LoadBalancerClient 解析名称(myservice 在 上一个示例)到一个实际的主机和端口,并替换 URI 相同的属性。

    默认情况下,当负载均衡器中找不到服务实例时 将返回 503。您可以配置网关返回 404 通过设置 spring.cloud.gateway.loadbalancer.use404=true。

    您也可以尝试在路线列表中指定顺序

    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
    

    【讨论】:

    • 谢谢,你的回答启发了我。我可能发现了问题
    猜你喜欢
    • 2018-04-07
    • 2018-12-10
    • 1970-01-01
    • 2015-07-31
    • 2020-01-28
    • 1970-01-01
    • 2016-11-19
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多