SpringBoot2.X 遭遇 No converter found for return value of type: class java.util.LinkedHashMap

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fxbin123/article/details/83221986

前几天,在项目上线时遭遇了 No converter found for return value of type: class java.util.LinkedHashMap 异常,异常的糟心唉。。。

场景重现
return 基本类型木有问题,但是return 对象的时候就报出了该问题,
网上答案汇总
1、缺失Jackson 依赖,如果有 spring-boot-starter-web依赖可排除该问题
2、是否是 POJO (get set 方法是否有)
: 我使用lombok插件,同样排除了此项可能性
3、注解是否是 @RestController


以上可能性完全排除,由于上线紧急,暂时采用了
HttpServletReponse.getWriter().print() 的方式

昨天在朋友帮助下,推测出是Swagger2 的问题,便没有在继续进行下去,然而一小时前采用一个demo进行测试排查是,发现并不是,继续昨天的思路,问题可能出现在拦截器那里,于是乎,开始从拦截器分析,就在刚刚,终于分析出了问题所在…

问题出现原因
真实原因是: Swagger2 访问资源被拦截,继承WebMvcConfigurationSupport 添加资源例外,之后国际化中文乱码,重写configureMessageConverters 方法修改编码,覆盖了默认方法,导致 converter found for return value of type xxx 问题

我重写的 configureMessageConverters 代码如下:

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

查看WebMvcConfigurationSupport 源码,最终发现是由于 没有添加 @EnableMvc 注解:

在这里插入图片描述

之后查看 @EnableMvc 注释的源码,在注释部分也充分说明了改注解的使用:一下部分摘自源码

/**
 * Adding this annotation to an {@code @Configuration} class imports the Spring MVC
 * configuration from {@link WebMvcConfigurationSupport}, e.g.:
 *
 * <pre class="code">
 * &#064;Configuration
 * &#064;EnableWebMvc
 * &#064;ComponentScan(basePackageClasses = MyConfiguration.class)
 * public class MyConfiguration {
 *
 * }
 * </pre>
 *
 * <p>To customize the imported configuration, implement the interface
 * {@link WebMvcConfigurer} and override individual methods, e.g.:
 *
 * <pre class="code">
 * &#064;Configuration
 * &#064;EnableWebMvc
 * &#064;ComponentScan(basePackageClasses = MyConfiguration.class)
 * public class MyConfiguration implements WebMvcConfigurer {
 *
 * 	   &#064;Override
 * 	   public void addFormatters(FormatterRegistry formatterRegistry) {
 *         formatterRegistry.addConverter(new MyConverter());
 * 	   }
 *
 * 	   &#064;Override
 * 	   public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
 *         converters.add(new MyHttpMessageConverter());
 * 	   }
 *
 * }
 * </pre>
 *
 * <p><strong>Note:</strong> only one {@code @Configuration} class may have the
 * {@code @EnableWebMvc} annotation to import the Spring Web MVC
 * configuration. There can however be multiple {@code @Configuration} classes
 * implementing {@code WebMvcConfigurer} in order to customize the provided
 * configuration.
 *
 * <p>If {@link WebMvcConfigurer} does not expose some more advanced setting that
 * needs to be configured consider removing the {@code @EnableWebMvc}
 * annotation and extending directly from {@link WebMvcConfigurationSupport}
 * or {@link DelegatingWebMvcConfiguration}, e.g.:
 *
 * <pre class="code">
 * &#064;Configuration
 * &#064;ComponentScan(basePackageClasses = { MyConfiguration.class })
 * public class MyConfiguration extends WebMvcConfigurationSupport {
 *
 * 	   &#064;Override
 *	   public void addFormatters(FormatterRegistry formatterRegistry) {
 *         formatterRegistry.addConverter(new MyConverter());
 *	   }
 *
 *	   &#064;Bean
 *	   public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
 *         // Create or delegate to "super" to create and
 *         // customize properties of RequestMappingHandlerAdapter
 *	   }
 * }
 * </pre>
 *

以上就是关于该问题的分析,以及解决方案

— end —

如有问题,请及时沟通,谢谢

猜你喜欢

转载自blog.csdn.net/fxbin123/article/details/83221986