【问题标题】:Multiple HTTP methods display for the spring fox health route in swaggerswagger 中显示 spring 狐狸健康路由的多种 HTTP 方法
【发布时间】:2016-08-06 04:45:19
【问题描述】:

我在 spring boot 项目中使用 spring boot actuator health with spring fox swagger。我在我的 Application.java 类中使用下面。

@Autowired
private HealthAggregator healthAggregator;

@Autowired
private Map<String, HealthIndicator> healthIndicators;

@Bean
public com.health.TestMeHealthEndpoint getHealthEndpoint() {
    return new com.health.TestMeHealthEndpoint(healthAggregator, healthIndicators);
}

@Bean
public Docket testMeApi() {
    return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false).apiInfo(apiInfo()).select()
            .paths(testMePaths()).build();
}

private Predicate<String> testMePaths() {
    return or(regex("/api/myservice1"), regex("/health"));
}

但是当我检查 swagger ui 时,我看到了多个健康端点,所有类型的 http 方法包括 POST、DELETE、OPTIONS 等。对于在 REST 控制器中实现的 myservice1,它只显示 GET 方法。

TestMeHealthEndpoint 扩展 AbstractEndpoint 并使用自定义健康信息覆盖调用方法。

我只想看看是不是健康路由的GET方法?

添加TestMeHealthEndpoint的来源:

@ConfigurationProperties(prefix = "endpoints.health", ignoreUnknownFields = true)
public class TestMeHealthEndpoint  extends AbstractEndpoint<Health> {

  //Some getter and setters for api name , version etc

  public TestMeHealthEndpoint (final HealthAggregator healthAggregator,
            final Map<String, HealthIndicator> healthIndicators) {
        super("health", false);
        final CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);
        for (final Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
            healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
        }
        this.healthIndicator = healthIndicator;
    }

  @Override
    public Health invoke() {
        final Health health = new Health();
        health.setStatus(this.healthIndicator.health().getStatus().getCode());
        health.setName(this.apiName);
        health.setVersion(this.apiVersion);
        final UriComponentsBuilder path = ServletUriComponentsBuilder.fromCurrentServletMapping()
                .path(this.managementContextPath).pathSegment(this.getId());
        health.add(new Link(path.build().toUriString()).withSelfRel());
        return health;
    }
}

【问题讨论】:

  • 请出示源代码TestMeHealthEndpoint
  • @shazin 请完善编辑问题
  • 这个 AbstractEndpoint 是什么?您使用的是哪个框架?
  • 弹簧靴执行器

标签: java spring spring-boot swagger spring-boot-actuator


【解决方案1】:

我想建议您一些解决方法。创建将请求委托给 Health 端点的休息控制器。像这样的:

@RestController
public class HealthController {

    @Autowired
    TestMeHealthEndpoint testMeHealthEndpoint;

    @ApiOperation(value="Health endpoint", notes = "Health endpoint")
    @RequestMapping(value = "/health", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
    public ResponseEntity<Health> invoke() {
        return ResponseEntity.ok(testMeHealthEndpoint.invoke());
    }
}

这样你也可以使用以下指令进行招摇:

.select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

【讨论】:

    【解决方案2】:

    Swagger 假设如果没有设置@RequestMapping 方法,任何方法都可以。将method = RequestMethod.GET 添加到您的RequestMapping 大括号()

    如果您添加一个端点类型的@Bean,那么它将自动通过 JMX 和 HTTP 公开(如果有可用的服务器)。通过创建 MvcEndpoint 类型的 bean,可以进一步自定义 HTTP 端点。您的 MvcEndpoint 不是 @Controller,但它可以使用 @RequestMapping(和 @Managed*)来公开资源。

    http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

    【讨论】:

    • TestMeHealthEndpoint 不是 RestController 知道的。所以我不能添加它
    • 如果我只使用带有 pom 依赖的弹簧执行器默认健康路由,它只会在点击 /health 端点时显示 UP。但是我需要添加其他数据,例如 api 名称、版本,以便我必须实现 TestMeHealthEndpoint
    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多