当前位置: 首页 > 知识库问答 >
问题:

Spring 5-jackson-dataformat-XML强制@responsebody with XML

长孙逸仙
2023-03-14

我必须处理一个遗留的Spring5[not boot]项目,并且我面临类似的问题,比如Jackson-DataFormat-XML将@responsebody转换为XML,所以我开始使用XMLMapper-input是application/text[这是XML字符串],而out是json在控制器中。但我一添加依赖项就看到了

<dependency>
<artifactId>jackson-dataformat-xml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<version>2.10.2</version>
</dependency>
@Configuration
public class HttpResponseConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));

    }
}

共有1个答案

施旭东
2023-03-14

您是否尝试指定响应类型?

@PostMapping(value = "/content", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody

https://www.baeldung.com/spring-request-response-body

 类似资料: