使用WebMvcConfigurationSupport后导致原来返回的json数据变为了xml的解决方法

问题

未使用WebMvcConfigurationSupport拦截时返回的数据都是JSON格式,使用WebMvcConfigurationSupport做拦截后数据的返回变为了XML的格式。

原因

在Spring框架中,WebMvcConfigurationSupport 是一个类,它可以用于自定义Spring MVC的配置。如果您在应用程序中使用了WebMvcConfigurationSupport,而且之前返回的JSON数据现在变成了XML,那么很可能是由于您在配置中进行了一些变更,导致了默认的消息转换器发生了改变。

默认情况下,Spring使用MappingJackson2HttpMessageConverter作为消息转换器,它可以将Java对象转换为JSON格式的数据。如果您在继承WebMvcConfigurationSupport类时没有明确指定消息转换器,可能会导致Spring使用其他默认的消息转换器,比如Jaxb2RootElementHttpMessageConverter,这个转换器可以将Java对象转换为XML格式的数据。

解决办法

java 复制代码
@Configuration
public class CustomWebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
        super.configureMessageConverters(converters);
    }
}
相关推荐
一 乐6 分钟前
智慧医药|基于springboot + vue智慧医药系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
悟空码字27 分钟前
SpringBoot接口防抖大作战,拒绝“手抖”重复提交!
java·spring boot·后端
与遨游于天地44 分钟前
Spring 的10个核心能力,对框架开发的启示
java·后端·spring
计算机毕设指导61 小时前
基于微信小程序的垃圾分类信息系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
韩立学长3 小时前
【开题答辩实录分享】以《植物园信息管理系统》为例进行选题答辩实录分享
java·数据库·spring
我认不到你3 小时前
自定义注解实现 Redis Stream 消息监听
spring boot·redis
Knight_AL3 小时前
Spring Boot 的主要特性与传统 Spring 项目的区别
spring boot·后端·spring
无名无姓某罗3 小时前
jQuery 请求 SpringMVC 接口返回404错误排查
前端·spring·jquery
黄俊懿3 小时前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
用户2190326527354 小时前
别再到处try-catch了!SpringBoot全局异常处理这样设计
java·spring boot·后端