Skip to content

第三方组件适配

Swagger/Springdoc

参考example(https://github.com/feiniaojin/graceful-response-example)工程配置。

yaml
graceful-response:
  exclude-packages:
    - springfox.**
  exclude-urls:
    - /**/api-docs/**

spring-boot-starter-actuator

需要满足下面两点:

  • Graceful Response版本>=3.2.1

  • application.yaml配置以下属性:

yaml
graceful-response:
  exclude-packages:
    - org.springframework.boot.actuate.*

FastJson2

第一步、配置WebMvcConfigurer

自3.5.0开始,支持使用FastJsonHttpMessageConverter进行JSON序列化。

java
@Configuration
public class ExampleConfig implements WebMvcConfigurer {
    
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    //自定义配置...
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
    config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(StandardCharsets.UTF_8);
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    converters.add(0, converter);
  }
}

注:configureMessageConverters中的代码来自 https://github.com/alibaba/fastjson2/blob/main/docs/spring_support_cn.md

第二步、指定HttpMessageConverter

spring6、Boot3采用以下配置:

yaml
graceful-response: 
  json-http-message-converter: com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter

spring5、Boot2采用以下配置:

yaml
graceful-response: 
  json-http-message-converter: com.alibaba.fastjson.support.spring6.http.converter.FastJsonHttpMessageConverter