Closing swagger2 causes interface exception No converter found for return value of type...

abnormal

Scenario: Close swagger2 when a project with a normal interface goes online.

Problem: The interface that uses a custom entity class to return data will have the following exception:

Cause Analysis

Project configuration

There is such a configuration class in the project:

class MyWebConfig extends WebMvcConfigurationSupport

The following code in the class:

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //解决中文乱码
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        //添加 stringHttpMessageConverter
        converters.add(stringHttpMessageConverter);
    }

The original intention of this configuration is to solve the problem of Chinese garbled characters in the interface that directly returns String (the default encoding of StringHttpMessageConverter is ISO-8859-1)

The crux of the problem lies in this rewriting method.

 

Why is swagger2 normal when it is turned on?

The interface was normal before swagger2 was closed because swagger processed converters and the interface was normal.

After closing swagger2, the configureMessageConverters method will cause the default converts to fail to initialize.

Note: There is no analysis here about how swagger is processed.

 

Source code analysis through WebMvcConfigurationSupport

Code snippets in the WebMvcConfigurationSupport class:

protected final List<HttpMessageConverter<?>> getMessageConverters() {
        if (this.messageConverters == null) {
            this.messageConverters = new ArrayList();
            this.configureMessageConverters(this.messageConverters);
            if (this.messageConverters.isEmpty()) {
                this.addDefaultHttpMessageConverters(this.messageConverters);
            }

            this.extendMessageConverters(this.messageConverters);
        }

        return this.messageConverters;
    }

The configureMessageConverters method is rewritten in the project and addconverters will cause

This.ddDefaultHttpMessageConverters(this.messageConverters) method is not executed.

As a result, the default converts cannot be added to the messageConverters, which is the cause of the exception.

Solution

Comment out the code that rewrites configureMessageConverters

Override method extendMessageConverters

protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }

 Change the default encoding of StringHttpMessageConverter

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.extendMessageConverters(converters);
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof StringHttpMessageConverter) {
                StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) converter;
                stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
    }

After this modification, there is no garbled problem in returning String Chinese, and the interface returning to the entity is also normal.

Guess you like

Origin blog.csdn.net/cs373616511/article/details/105832543