kap*_*kap 23 spring-boot spring-boot-actuator
我已经将Spring Boot应用程序从1.4.x更新到1.5.1,Spring Actuator端点现在返回不同的MIME类型:
例如,/health现在application/vnd.spring-boot.actuator.v1+json简单地说application/json.
我怎么能改回来?
And*_*son 17
端点返回一个内容类型,该内容类型表示客户端的请求可以接受的内容.application/json如果客户端发送Accept要求它的标头,您将收到响应:
Accept: application/json
Run Code Online (Sandbox Code Playgroud)
loï*_*oïc 14
响应/sf/users/206646541/的评论(我的声誉很低,以创建评论):当使用Firefox检查返回JSON的端点时,我使用Add-on JSONView.在设置中有一个选项来指定备用JSON内容类型,只需添加application/vnd.spring-boot.actuator.v1+json,您将在浏览器中看到返回的JSON.
小智 6
自 SpringBoot 2.0.x 以来,实现中建议的解决方案EndpointHandlerMappingCustomizer不再有效。
好消息是,现在解决方案更简单了。
EndpointMediaTypes需要提供Bean 。它是由SpringBoot类WebEndpointAutoConfiguration默认提供的。
提供您自己的可能如下所示:
@Configuration
public class ActuatorEndpointConfig {
private static final List<String> MEDIA_TYPES = Arrays
.asList("application/json", ActuatorMediaType.V2_JSON);
@Bean
public EndpointMediaTypes endpointMediaTypes() {
return new EndpointMediaTypes(MEDIA_TYPES, MEDIA_TYPES);
}
}
Run Code Online (Sandbox Code Playgroud)
您注意到执行器的内容类型在1.5.x中已更改.
如果你将"application/json"放在"Accept:"标题中,你应该得到通常的内容类型.
但是,如果您没有任何修改客户端的方法,则此代码段将返回运行状况(无详细信息)和原始内容类型(1.4.x方式).
@RestController
@RequestMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE)
public class HealthController {
@Inject
HealthEndpoint healthEndpoint;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Health > health() throws IOException {
Health health = healthEndpoint.health();
Health nonSensitiveHealthResult = Health.status(health.getStatus()).build();
if (health.getStatus().equals(Status.UP)) {
return ResponseEntity.status(HttpStatus.OK).body(nonSensitiveHealthResult);
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(nonSensitiveHealthResult);
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置(移走现有的健康状况)
endpoints.health.path: internal/health
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7912 次 |
| 最近记录: |