---
title: "Springboot 2.0 Health接口状态控制"
author: "QING____"
date: "2019-03-30T15:40:43Z"
tags: []
categories: ["编程语言"]
slug: "springboot-2-0-health"
draft: false
lastmod: "2019-03-30T15:40:43Z"
id: 2439567
url: "https://shift-alt-ctrl.iteye.com/blog/2439567"
---

Health接口，内部有个设计实现，当health接口的status不是UP时，访问health接口的http状态码为503，这个方式也挺好，对于一些工具而言（比如nginx 健康检查）比较方便，因为通过状态码就可以知道服务的健康情况。但是有时候，我们更希望通过health接口的返回内容来判断起实际状态，这个时候如果状态码不是200的话，可能影响请求解析。

 

    我们可以通过如下代码，来自定义health接口的status与http状态码的关系。

 

```
    @Bean
    @ConditionalOnProperty("management.health.status.static")
    public HealthStatusHttpMapper healthStatusHttpMapper() {
        HealthStatusHttpMapper healthStatusHttpMapper = new HealthStatusHttpMapper();
        healthStatusHttpMapper.addStatusMapping(Status.DOWN,WebEndpointResponse.STATUS_OK);
        healthStatusHttpMapper.addStatusMapping(Status.UNKNOWN,WebEndpointResponse.STATUS_OK);
        healthStatusHttpMapper.addStatusMapping(Status.OUT_OF_SERVICE,WebEndpointResponse.STATUS_OK);
        return healthStatusHttpMapper;
    }
```