SpringDoc-OpenApi Conflicts with Fastjson - Default handling of String by FastJsonHttpMessageConverter

In my project, FastJsonHttpMessageConverter is used by default as follows:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            // Prevent circular references
            SerializerFeature.DisableCircularReferenceDetect,
            // Empty collection returns [], not null
            SerializerFeature.WriteNullListAsEmpty,
            // An empty string returns' ', not null
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteMapNullValue
    );
    fastJsonConfig.getSerializeConfig().put(String.class,MyStringSerializer.instance);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    //Dealing with Chinese garbled code
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    converters.add(0, fastJsonHttpMessageConverter);//Put it in front

}

Previously, Xiang'an ignored it, but later, spring boot was upgraded to 2.6 x. Because the original springfox has not been updated for a long time, there is a compatibility problem (although it can be solved). In addition, I want to try to use the new openapi3, so I use the recently updated springdoc OpenAPI. Results when the two are used together, the swagger interface cannot be loaded, and there are more json in V3 / API docs \:

"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"http://localhost:8099\",\"description\":\"Generated server url\"}],\"paths\":{},\"components\":{}}"

This directly leads to the failure of swagger to correctly identify and load the interface. This problem did not occur when using spring fox.

After the break point analysis, it is found that when using springfox, it is transmitted to com alibaba. fastjson. serializer. Serializeconfig #getobjectwriter (Java. Lang. class <? >, Boolean) here, the class is springfox documentation. spring. web. json. Jason, fastjson has made special treatment for this, so it can be kept as is. When changing springdoc, the class passed in here is String! Fastjson handles String by default and adds escape symbols, which leads to exceptions in the final result.
terms of settlement:

1. Change to Jackson's converter (springboot is Jackson by default).

2. FastJsonHttpMessageConverter: customize a StringSerializer and override the default StringSerializer of fastjson

public class MyStringSerializer implements ObjectSerializer {
    public static final MyStringSerializer instance = new MyStringSerializer();
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.getWriter();
        out.write(object.toString());
    }
}

Then, in the configuration file of FastJsonHttpMessageConverter, use
fastJsonConfig.getSerializeConfig().put(String.class,MyStringSerializer.instance);
You can override the fastjson default StringSerializer. (you can also customize other classes by doing this.).
Because the understanding is not very thorough, I don't know why fastjson needs to escape the character of String by default, or whether such modification will lead to other consequences. If it is inappropriate, I hope to point out.

(this bug has been submitted to fastjson's issues on github. I hope it can be solved, but it seems that it can only be solved by analyzing the content in the String...)

This bug is only caused when FastJsonHttpMessageConverter is added to the front of converters or before the default Jackson. For more in-depth discussion on the order of converters, please refer to this article:

https://segmentfault.com/a/1190000012659486

Added by lemming_ie on Tue, 18 Jan 2022 11:49:50 +0200