慎用@EnableWebMvc注解

同事合了代码到开发分支,并没有涉及到改动的类却报错。错误信息如下:

java 复制代码
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.http.converter.HttpMessageConversionException: 
Type definition error: [simple type, class com.tongweb.demo.bean.Registration];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.tongweb.demo.bean.Registration` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

这个错误的大概意思就是jackson解析Registration类时出现了错误,说Registration类缺少默认构造函数。这个类是Controller中一个方法的入参。示例代码如下:

java 复制代码
@PostMapping(path = "/instances", consumes = MediaType.APPLICATION_JSON_VALUE)  
public String register(@RequestBody Registration registration) {  
    Registration withSource = Registration.copyOf(registration).source("http-api").build(); 
    return withSource.toString();  
}

相关代码并不涉及到改动,自从我同事代码合过来就开始报错,很不应该。起初我猜测是某些jar冲突引起的。但是查看了他合并过来的代码,也并未涉及到jar依赖的升级变更等。对于这个错误网上是有很多解决方案的,比如加个构造函数。但是不排查到导致问题的原因,这总归是个雷。于是我开启了漫长的排除过程。

首先考虑是jar冲突导致。我重新写了一个新项目,沿用了我目前的所有jar。访问instances接口,jackson解析并不会报错。那只有跟踪源码了。

spring提供了一个Jackson的HTTP消息转换器的配置JacksonHttpMessageConvertersConfiguration,会创建一个MappingJackson2HttpMessageConverterMappingJackson2HttpMessageConverter会负责解析Registration类。特别说明Registration类只有有参构造函数,没有无参构造函数。

HttpMessageConverters负责管理Spring Boot应用程序中使用的HttpMessageConverters。提供了一种向web应用程序添加和合并其他HttpMessageConverter的方便方法。 如果需要,可以向特定的附加转换器注册此bean的实例,否则将使用默认转换器。说白了就是Spring Boot应用程序中的各个转换就是在HttpMessageConverters中。

按理来说MappingJackson2HttpMessageConverter也是HttpMessageConverters中的一员。但是我跟踪源码发现MappingJackson2HttpMessageConverter并未出现在HttpMessageConverters中。跟踪源码WebMvcConfigurationSupport类,源码如下:

java 复制代码
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {  
    messageConverters.add(new ByteArrayHttpMessageConverter());  
    messageConverters.add(new StringHttpMessageConverter());  
    messageConverters.add(new ResourceHttpMessageConverter());  
    messageConverters.add(new ResourceRegionHttpMessageConverter());
    ...
}

这一段关键代码是把HttpMessageConverters添加到messageConverters。后续解析@RequestBody修饰的类就会从中选择对应的Converter。当源码进行到这一步的时候。我发现MappingJackson2HttpMessageConverter并不存在。它的加载顺序发生在了addDefaultHttpMessageConverters方法执行之后。这就导致messageConverters中并没有MappingJackson2HttpMessageConverter。所以当@RequestBody修饰的类没有无参构造函数的时候解析就报错了。一个正常的应用程序MappingJackson2HttpMessageConverter会优先被加载。找到问题的原因就方便多了。

为什么JacksonHttpMessageConvertersConfiguration的加载顺序会晚于WebMvcConfigurationSupport类呢?换而言之有哪些因素会影响WebMvcConfigurationSupport类的加载。答案就是@EnableWebMvc注解!!

如果在代码中使用了@EnableWebMvc注解,那么WebMvcConfigurationSupport就会比JacksonHttpMessageConvertersConfiguration优先加载,因为@EnableWebMvc注解会导入DelegatingWebMvcConfiguration类,而这个类继承了WebMvcConfigurationSupport类,并且有@Order注解,指定了加载顺序。

这样的话,Spring Boot的默认MVC配置就会被关闭,你需要自己配置Spring MVC的功能,例如拦截器、视图解析器、消息转换器等。

相关推荐
wno7042 小时前
Spring Boot JdbcTemplate配置Druid多数据源
java·spring boot·后端
wear工程师2 小时前
Spring Boot 服务挂了却没重启?先分清 systemd 的三次判断
spring boot
聊浮游2 小时前
JAVA2026最新全套学习资料、学习路线
java·开发语言·jvm·mysql·spring·maven·idea
旺仔学长 哈哈3 小时前
52486|高校专业评估不再靠表格:Spring Boot 本科专业质量评估管理系统实战
数据库·spring boot·毕业设计·教育管理
未秃头的程序猿3 小时前
读Spring AI源码的方法——不是硬啃,是带着问题去翻
java·后端·spring
evans在进步3 小时前
Spring MVC 核心机制详解:全局异常处理、请求跳转与数据绑定
java·spring·mvc
weixin_BYSJ19873 小时前
springboot小区物业管理小程序---附源码45555
java·javascript·spring boot·python·django·flask·php
RuoyiOffice4 小时前
抓包还能看到密码?Vue3 + Spring Boot 3 接口 AES/RSA 加解密(@ApiEncrypt)全链路实战
spring boot·vue3·aes·rsa·接口安全·spring boot 3·apiencrypt
凤山老林4 小时前
零停机数据库演进:Spring Boot 集成 Flyway 与平滑DDL变更策略
数据库·spring boot·后端
抓哇小菜鸡4 小时前
Spring Boot + 本地大模型(Ollama/DeepSeek) + MyBatis-Plus 企业级智能体数据分析系统从零到一源码全解析
spring boot·后端·mybatis