我已将 Spring Boot 应用程序从 1.4.x 更新到 1.5.1,Spring Actuator 端点现在返回不同的 MIME 类型:
例如,
/health
现在是 application/vnd.spring-boot.actuator.v1+json
,而只是 application/json
。
我怎样才能把它改回来?
端点返回一个内容类型,该内容类型遵循客户端请求所说的它可以接受的内容。如果客户端发送请求的
application/json
标头,您将收到 Accept
响应:
Accept: application/json
响应https://stackoverflow.com/users/2952093/kap的评论(我的声誉太低,无法创建评论):当使用 Firefox 检查返回 JSON 的端点时,我使用附加 JSONView 。在设置中,有一个选项可以指定备用 JSON 内容类型,只需添加
application/vnd.spring-boot.actuator.v1+json
,您就会在浏览器中以漂亮的打印形式看到返回的 JSON。
自 SpringBoot 2.0.x 起,实现
EndpointHandlerMappingCustomizer
时建议的解决方案不再有效。
好消息是,现在解决方案更简单了。
需要提供Bean
EndpointMediaTypes
。默认由 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);
}
}
正如您注意到的,执行器的内容类型在 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);
}
}
}
配置(移走现有的健康状况)
endpoints.health.path: internal/health
基于https://github.com/spring-projects/spring-boot/issues/2449中的代码(也可以正常工作,但完全删除了新类型)我想出了
@Component
public class ActuatorCustomizer implements EndpointHandlerMappingCustomizer {
static class Fix extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Object attribute = request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
if (attribute instanceof LinkedHashSet) {
@SuppressWarnings("unchecked")
LinkedHashSet<MediaType> lhs = (LinkedHashSet<MediaType>) attribute;
if (lhs.remove(ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON)) {
lhs.add(ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON);
}
}
return true;
}
}
@Override
public void customize(EndpointHandlerMapping mapping) {
mapping.setInterceptors(new Object[] {new Fix()});
}
}
将新的供应商媒体类型放在最后,以便在未指定任何内容时它将使用
application/json
作为all执行器端点。
使用 spring-boot 1.5.3 进行测试
要在 Firefox 内置 JSON 查看器中支持
application/vnd.spring-boot.actuator.v1+json
,您可以安装此插件:json-content-type-override。它将包含“json”的内容类型转换为“application/json”。
更新: Firefox 58+ 内置了对这些 mime 类型的支持,不再需要任何插件。请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=1388335