spring boot2 configuration FastJsonHttpMessageConverter does not work

Keywords: JSON xml

Project uses custom FastJsonHttpMessageConverter for API data response JSON converter

In the original version of springboot1.X, it can take effect. The configuration is as follows:

/**
     * Instead of using FastJson to parse the returned results
     */
    @Override  
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {  
        /**
         * 1.First define an object to convert the message
         * 2.Add the configuration information of fastjson, such as whether to format the returned json data
         * 3.Add configuration information to convert
         * 4.Add convert to converters
         */
        //1. Define an object of convert conversion message first
        FastJsonJsonpHttpMessageConverter fastConverter = new FastJsonJsonpHttpMessageConverter();
        //2. Add the configuration information of fastjson, such as whether to format the returned json data
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        
        //Deal with Chinese code scrambling (otherwise Chinese code scrambling will occur)
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        
        //3. Add configuration information to convert
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4. Add convert to converters
        converters.add(fastConverter);
    } 

After upgrading to springboot2, the response FastJsonHttpMessageConverter does not work. According to the understanding of springmvc, if the customization does not work, the default message converter should be used. To verify the idea, add the following code in the configuration to print out the list of message Converters:

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> messageConverter : converters) {
            System.out.println(messageConverter); //2
        }
    }

Output the following information:

org.springframework.http.converter.ByteArrayHttpMessageConverter@433e3357
org.springframework.http.converter.StringHttpMessageConverter@4a1066a2
org.springframework.http.converter.StringHttpMessageConverter@37531849
org.springframework.http.converter.ResourceHttpMessageConverter@1cc4de0
org.springframework.http.converter.ResourceRegionHttpMessageConverter@54629d11
org.springframework.http.converter.xml.SourceHttpMessageConverter@66f4a841
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@49ca7586
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@61a93e7c
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2a0a9212
com.dreamer.core.converter.FastJsonJsonpHttpMessageConverter@6a400857

You can see that the custom message converter [FastJsonJsonpHttpMessageConverter] is at the end of the list.

According to the application rules of the message converter, the consumer converters that meet the requirements will be selected in sequence. MappingJackson2HttpMessageConverter is in front of FastJsonJsonpHttpMessageConverter, so that the consumer converter will be used. To confirm that the idea is correct, I will use MappingJackson2HttpMessageConverter - > writeInternal The method is debug ged. After running, it is consistent with the idea.

To find out the reason, just add the custom message converter [FastJsonJsonpHttpMessageConverter] to the front of mappingjackson 2httpmessageconverter. There is another way to configure the custom message converter, as follows:

 @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
    	/**
         * 1.First define an object to convert the message
         * 2.Add the configuration information of fastjson, such as whether to format the returned json data
         * 3.Add configuration information to convert
         * 4.Add convert to converters
         */
        //1. Define an object of convert conversion message first
        FastJsonJsonpHttpMessageConverter fastConverter = new FastJsonJsonpHttpMessageConverter();
        //2. Add the configuration information of fastjson, such as whether to format the returned json data
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        
        //Deal with Chinese code scrambling (otherwise Chinese code scrambling will occur)
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        
        //3. Add configuration information to convert
        fastConverter.setFastJsonConfig(fastJsonConfig);
        
        return new HttpMessageConverters(fastConverter);
    }

After configuration, restart the project and see that the message converter list in the project prints the following:

com.dreamer.core.converter.FastJsonJsonpHttpMessageConverter@75652058
org.springframework.http.converter.ByteArrayHttpMessageConverter@30fa29ef
org.springframework.http.converter.StringHttpMessageConverter@7749f8a4
org.springframework.http.converter.ResourceHttpMessageConverter@5351785e
org.springframework.http.converter.ResourceRegionHttpMessageConverter@f6af58c
org.springframework.http.converter.xml.SourceHttpMessageConverter@6ebe10df
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@560a4974
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@e415863

Here you can see that the user-defined message converter [FastJsonJsonpHttpMessageConverter] is in front of mappingjackson 2httpmessageconverter and runs the API at the same time. debug is to run FastJsonJsonpHttpMessageConverter - > writeInternal method.

Good Bye

Posted by psymonic on Fri, 03 Jan 2020 17:42:08 -0800