Spring Boot执行器端点的响应MIME类型

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)

  • 我也被这个咬了.请问为什么会改变?更改我们公司使用的所有浏览器的设置有点像PITA.我还没有发现如何在Safari或智能手机上的浏览器中执行此操作.这种变化是否与以下问题有关?https://github.com/spring-projects/spring-boot/issues/7648 (4认同)
  • 好吧,这至少适用于我自己的应用程序和其他表现良好的应用程序.对于Firefox我必须寻找一些设置,它总是会下载端点而不是显示它们.谢谢. (3认同)
  • @kap Firefox的技巧是新版本的浏览器有一个很好的内置JSON查看器,但不承认`application/vnd.spring-boot.actuator.v1 + json`是一个真正的JSON类型.您可以使用JSONView插件,以检查两个选项有:`包含在HTTP"应用/ JSON的"接受内置Firefox的JSON viewer`为requests`头和`使用.这将使浏览器在响应中要求`application/json`类型,但JSONView不会干扰读取响应,它将被Firefox读取为普通JSON. (3认同)

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)


hir*_*rro 5

您注意到执行器的内容类型在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)