java - swagger中spring fox health路由显示多个HTTP方法

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

我在一个 spring boot 项目中使用 spring boot actuator health 和 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 Controller 中实现的 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;
    }
}

最佳答案

我想建议您一些解决方法。创建将请求委托(delegate)给 Health 端点的 rest Controller 。像这样:

@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());
    }
}

通过这种方式,您还可以使用以下指令来 Swagger :

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

关于java - swagger中spring fox health路由显示多个HTTP方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38800625/

相关文章:

java - 使用 Spring Security 的多个用户

java - JRadioButton 转换为 boolean 值?

java - 从一种时间格式转换为另一种时间格式

java - 将 Spring Batch 导出到 JAR 时如何外部化 XML 文件

java - 将图像上传到java spring

java - spring 集成中的自定义并发 TcpOutboundGateway

java - 使用 JSP 作为 View 引擎注销的 Spring Boot 安全性不起作用

spring-boot - 在 Eureka 服务器 UI 中以名称 "UNKNOWN"运行的 Eureka 客户端

java - 组织.postgresql.util.PSQLException : ERROR: value too long for type character varying(255)

java - 通过在 Java 中测试 int 数组的值来创建一个 char 数组