gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:02 24 4
gpt4 key购买 nike

我在一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com