使用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);
    }
}
相关推荐
Monly213 小时前
RabbitMQ:生产者可靠性(生产者重连、生产者确认)
spring boot·rabbitmq·java-rabbitmq
ciku4 小时前
Spring Ai Advisors
人工智能·spring·microsoft
ankleless4 小时前
Spring Boot 实战:从项目搭建到部署优化
java·spring boot·后端
白露与泡影7 小时前
SpringBoot前后端token自动续期方案
spring boot·后端·状态模式
还听珊瑚海吗9 小时前
基于WebSocket和SpringBoot聊天项目ChatterBox测试报告
spring boot·websocket·网络协议
Monly2110 小时前
RabbitMQ:SpringAMQP Topic Exchange(主题交换机)
spring boot·rabbitmq·java-rabbitmq
杨杨杨大侠12 小时前
Spring AI Alibaba Multi-Agent 架构详解
spring·aigc
ademen14 小时前
spring第9课,spring对DAO的支持
java·数据库·spring
嵌R式小Z15 小时前
JSON&cJSON
json