springboot接收并响应xml

本文介绍了如何在SpringBoot应用中接收和响应XML数据。首先讲解了需要引入的依赖,然后配置XML转换器,接着展示了在Controller层的处理方法。针对响应XML缺少头部信息的问题,文中提供了解决方案,通过设置xmlMapper的配置项来写入XML声明。最后,文章还探讨了处理复杂XML结构的方法和示例。

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

一、引包

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.9.7</version>
</dependency>

二、配置XML转换器

@Configuration
public class MessageConverterConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
        builder.indentOutput(true);
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
    }
}

三、controller层

@RequestMapping(value = "/creditLoanApplyNotify", method = RequestMethod.POST,consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public String creditLoanApplyNotify(@RequestBody CreditLoanApplyNotifyDto req) {
// 自己逻辑
}

四、响应的XML没有xml头

这种:<?xml version="1.0" encoding="UTF-8"?>

起作用的是: xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);

 XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setDefaultUseWrapper(false);
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        String requestBody  = xmlMapper.writeValueAsString(req);

五、对于复杂的xml

比如xml长这样:

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <response>
        <head>
            <appId>ANK85</appId>
            <function>apply.notify</function>
            <inputCharset>UTF-8</inputCharset>
            <reqMsgId>201803300309471966</reqMsgId>
            <reserve>1.0.0.20180621</reserve>
            <respTime>201909191</respTime>
            <respTimeZone>UTC+8</respTimeZone>
            <signType>RSA</signType>
            <version>1.0.0</version>
        </head>
        <body>
            <applyNo>2018033</applyNo>
            <requestId>A</requestId>
            <resultInfo>
                <resultCode>000</resultCode>
                <resultMsg>sign check fail</resultMsg>
                <retry>N</retry>
            </resultInfo>
        </body>
    </response>
    <signature>arfLohOxOIL0BOry==</signature>
</document>

处理示例:

@JacksonXmlRootElement(localName = "document")
@Data
public class CreditLoanApproveAckNotifyDto {
    @JacksonXmlElementWrapper(localName ="request")
    private Request request;
    @JacksonXmlProperty(localName = "signature")
    private String signature;

    @Data
    public class Request {
        @JacksonXmlElementWrapper(localName ="head")
        private Header head;
        @JacksonXmlElementWrapper(localName ="body")
        private NotifyDomain body;
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值