解决No converter for[xxx] with preset Content-Type XXX 问题

===请求异常==:No converter for [class org.springframework.boot.actuate.health.SystemHealth] with preset Content-Type 'null'

{"code":500,"info":"No converter for [class org.springframework.boot.actuate.health.SystemHealth] with preset Content-Type 'null'","message":"系统错误"}

Spring boot在添加spring-boot-starter-actuator时,请求:spring-boot-starter-actuator访问报错。

!-- 检查测试 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

这个问题已经提示非常明确了,就是转换器不知道类型。只需要加入你要的类型就可以了。

@EnableWebMvc
@Configuration
class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public HttpMessageConverter<Object> fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        // 创建并配置 FastJsonConfig
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 格式设置
        fastJsonConfig.setDateFormat(pattern);
        fastJsonConfig.setJSONB(true);
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        // fastJsonConfig.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
        // fastJsonConfig.setWriterFeatures(JSONWriter.Feature.FieldBased, JSONWriter.Feature.WriteMapNullValue);
        // 设置 FastJsonConfig 到 FastJsonHttpMessageConverter
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        // 支持的媒体类型列表
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        // supportedMediaTypes.add(MediaType.ALL);
        supportedMediaTypes.add(MediaType.parseMediaType("application/vnd.spring-boot.actuator.v3+json"));
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);

        return fastJsonHttpMessageConverter;
    }

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        return new StringHttpMessageConverter();
    }

    @Bean
    public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
        return new ByteArrayHttpMessageConverter();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 增加二进制转换器,解决Spring doc的问题。
        converters.add(byteArrayHttpMessageConverter());
        // 增加string转换器
        converters.add(stringHttpMessageConverter());
        // 添加 FastJsonHttpMessageConverter 到 Spring MVC 的转换器列表中
        converters.add(fastJsonHttpMessageConverter());
    }
}

其中关键部分:supportedMediaTypes.add(MediaType.parseMediaType("application/vnd.spring-boot.actuator.v3+json"));就可以了。

{
    "components": {
        "custom": {
            "details": {
                "CustomService": "Up and running"
            },
            "status": "UP"
        },
        "db": {
            "details": {
                "database": "MySQL",
                "validationQuery": "isValid()"
            },
            "status": "UP"
        },
        "diskSpace": {
            "details": {
                "total": 95990837248,
                "free": 15276441600,
                "threshold": 10485760,
                "path": "C:\\Users\\liuwenjiang\\Desktop\\local\\WeAdmin\\.",
                "exists": true
            },
            "status": "UP"
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "details": {
                "version": "7.0.15"
            },
            "status": "UP"
        }
    },
    "status": "UP"
}

在application.yml中配置

management:
  endpoint:
    health:
      show-details: always
      enabled: true
  endpoints:
    web:
      exposure:
        include: health
      base-path: /actuator

再自定义一个自己的指示器:

/*
 * Copyright [2025-present] [Liu Rongming]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wewetea.developer.framework.core;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        // 实现自定义的健康检查逻辑
        if (isCustomServiceUp()) {
            return Health.up().withDetail("CustomService", "Up and running").build();
        } else {
            return Health.down().withDetail("CustomService", "Not available").build();
        }
    }
    private boolean isCustomServiceUp() {
        // 实现自定义服务的检查逻辑
        return true;
    }
}

就能输出上面的正常的结果了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。