【SpringBoot】FastJsonHttpMessageConverter 自定义转换器不生效的问题

文章讲述了在使用FastJsonHttpMessageConverter时,如何避免WebMvcConfigurer在继承WebMvcConfigurationSupport的情况下失效。作者建议将WebMvcConfigurationSupport改造为实现WebMvcConfigurer的类,以确保两者配置都能生效,并指出应避免同时使用两者,推荐单独使用WebMvcConfigurer。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:我在使用 FastJsonHttpMessageConverter 自定义转换器的时候,我是写在了实现 WebMvcConfigurer 的方法里面,但是我项目中还有一个继承了 WebMvcConfigurationSupport 类,导致只有 WebMvcConfigurationSupport 生效,WebMvcConfigurer 里面的不能生效。

解决办法:

  1. 首先看自己项目中 WebMvcConfigurationSupport 和 WebMvcConfigurer 是否同时存在了
  2. 将 WebMvcConfigurationSupport 改造成实现 WebMvcConfigurer 类

总结一下:

  • WebMvcConfigurationSupport 和 WebMvcConfigurer 都可以当作项目中的配置类,WebMvcConfigurer 是接口,WebMvcConfigurationSupport 是个类
  • 一个项目中只有一个 WebMvcConfigurationSupport 类会生效
  • 一个项目中可以有多个实现 WebMvcConfigurer 的类,并且都会生效
  • WebMvcConfigurationSupport 和 WebMvcConfigurer 同时使用的时候,只有WebMvcConfigurationSupport 中的配置会生效

不要同时使用 WebMvcConfigurationSupport 和 WebMvcConfigurer,推荐使用 WebMvcConfigurer。

参考文档:

WebMvcConfigurationSupport 和 WebMvcConfigurer 区别和同时使用产生的问题-解决

### 创建和使用 Spring Boot 自定义注解转换器Spring Boot 中,自定义注解转换器是一种强大的工具,可以用于简化复杂的数据处理逻辑。以下是关于如何实现这一功能的具体说明。 #### 1. 工作原理概述 Spring Boot消息转换机制基于 `HttpMessageConverter` 接口[^1]。该接口负责将 HTTP 请求体中的数据解析为 Java 对象或将 Java 对象序列化为响应体的内容。对于更复杂的业务需求,可以通过扩展此机制来自定义转换行为。而自定义注解则可以在方法参数或类属性层面提供额外的功能支持。 --- #### 2. 实现步骤详解 ##### 定义自定义注解 首先需要创建一个新的注解类,通常会指定它是一个运行时保留的元注解: ```java import java.lang.annotation.*; @Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation { String value() default ""; } ``` 上述代码片段展示了如何声明一个简单的自定义注解 `CustomAnnotation`,它可以应用于字段或者方法参数上[^3]。 --- ##### 编写对应的 Converter 或 Formatter 为了使这个注解生效,还需要编写具体的转换逻辑。这里可以选择两种方式之一:要么继承 `Converter<S,T>` 接口并重写其核心方法;要么利用 `Formatter<T>` 来完成字符串到目标类型的双向映射。 下面展示了一个典型的例子——将带有特定前缀的字符串转化为整数对象: ```java import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @Component public class CustomStringToIntegerConverter implements Converter<String, Integer> { private static final String PREFIX = "PREFIX_"; @Override public Integer convert(String source) { if (source.startsWith(PREFIX)) { try { return Integer.valueOf(source.substring(PREFIX.length())); } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid number format", e); } } return null; // 如果不符合预期格式,则返回null或其他默认值 } } ``` 在此基础上还可以进一步优化以适应更多样化的输入情况[^2]。 --- ##### 配置全局注册表 为了让 Spring 框架识别新加入的 converter ,需将其注入至 spring 上下文中并通过适当途径激活。一种常见做法是在配置文件里显式加载此类组件: ```java import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new CustomStringToIntegerConverter()); } } ``` 这样就完成了整个流程设置。 --- #### 3. 应用实例分析 假设存在如下 RESTful API 接收路径变量作为请求的一部分: ```java @GetMapping("/example/{id}") public ResponseEntity<?> getExample(@PathVariable @CustomAnnotation String id){ int convertedId = customService.process(id); ... } ``` 当客户端发送 `/example/PREFIX_123` 这样的 URL 访问时,系统内部将会调用我们之前定义好的 `CustomStringToIntegerConverter` 将原始字符串转成数值型 ID 后再继续执行后续操作。 --- #### 总结 通过以上介绍可以看出,在 Spring Boot 中构建自定义注解及其关联的消息转换器是一项既实用又灵活的技术手段。不仅可以提升代码可读性和维护效率,还能有效减少重复劳动带来的错误风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值