在swagger中显示多个HTTP方法用于Spring Fox健康路径

时间:2016-08-06 04:45:20

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

我在春季靴子项目中使用弹簧靴执行器健康与弹簧狐狸招摇。我在下面的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并覆盖invoke方法。

我只想看到健康路径的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;
    }
}

2 个答案:

答案 0 :(得分: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());
    }
}

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

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

答案 1 :(得分:0)

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

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

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