当前位置: 首页 > news >正文

springboot中关于继承WebMvcConfigurationSupport后自定义的全局Jackson失效解决方法,localdate返回数组问题

一般情况下我们在config里增加jackson的全局配置文件就能满足基本的序列化需求,比如前后端传参的问题。

@Configuration
public class JacksonConfig {public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";@Beanpublic ObjectMapper objectMapper() {ObjectMapper objectMapper = new ObjectMapper();JavaTimeModule javaTimeModule = new JavaTimeModule();javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN)));javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN)));javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN)));javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN)));javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));objectMapper.registerModule(javaTimeModule);objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));return objectMapper;}}

但是最近做的项目中发现,请求拦截的时候继承WebMvcConfigurationSupport排除某些例如swagger等目录的情况,上面的配置会失效,所以我们要稍微改动下。看WebMvcConfigurationSupport源码就能发现里面有自己的Jackson处理方法。我们只需要将配置也添加进去就行。

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {@Beanpublic HandlerInterceptor getTokenInterceptor() {return new TokenInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(getTokenInterceptor()).addPathPatterns("/**").addPathPatterns("/**")//排查swagger的相关的拦截.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");super.addInterceptors(registry);}/*** 排查swagger的相关的拦截** @param registry*/@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}@Overrideprotected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));}public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";public ObjectMapper objectMapper() {ObjectMapper objectMapper = new ObjectMapper();JavaTimeModule javaTimeModule = new JavaTimeModule();javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN)));javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN)));javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN)));javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN)));javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));objectMapper.registerModule(javaTimeModule);objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));return objectMapper;}
}

也就是把以前直接@Bean注入的形式改成了直接在子类Override然后add进去就行。LocalDateTime返回变成数组也是这个原因。

http://www.lryc.cn/news/143323.html

相关文章:

  • LeetCode 面试题 02.03. 删除中间节点
  • Redis知识点总结
  • (四)k8s实战-服务发现
  • AxureRP制作静态站点发布互联网,内网穿透实现公网访问
  • [Go版]算法通关村第十四关白银——堆高效解决的经典问题(在数组找第K大的元素、堆排序、合并K个排序链表)
  • 『FastGithub』一款.Net开源的稳定可靠Github加速神器,轻松解决GitHub访问难题
  • 软件开发的201个原则 阅读笔记 第172-201个原则
  • vue 后台管理系统登录 记住密码 功能(Cookies实现)
  • elementUI moment 年月日转时间戳 时间限制
  • 用AI + Milvus Cloud搭建着装搭配推荐系统教程
  • 大数据领域都有什么发展方向
  • NSSCTF——Web题目1
  • VScode中写Verilog时,iverilog语法自动纠错功能不起作用
  • thinkphp6.0 配合shell 脚本 定时任务
  • 18-使用钩子函数判断用户登录权限-登录前缀
  • 关闭win11启动界面搜索推荐
  • 中文乱码处理
  • JAVA坦克大战游戏v3
  • 使用acme,自动续签免费的SSL,无忧http升级https
  • Uniapp笔记(五)uniapp语法4
  • 编写一个yolov5的模型检测,只要运行后,就不结束,只要有文件放入到文件夹中,就去执行读取
  • vscode调试PHP代码
  • js reverse实现数据的倒序
  • 日常踩坑记录
  • threejs特殊几何体(一:文字几何体对象)
  • 链表的实现
  • c++ std::mutex与std::condition_variable
  • Aspose.Tasks for .NET V23Crack
  • vue过渡及动画
  • Linux环境下SVN服务器的搭建与公网访问:使用cpolar端口映射的实现方法