SpringBoot 接收xml数据

1、搭建SpringBoot项目,使用Maven管理只需要添加如下依赖即可。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
                <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、添加启动类

package com.mulhyac.lottery.ticket.boot;

import com.mulhyac.lottery.ticket.boot.ticket.Ticket;
import com.mulhyac.lottery.ticket.boot.ticket.TicketFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}

这样boot项目就创建完毕了。

3、添加xml依赖

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

4、编写数据请求、响应类

⑴TicketRequest.java
package com.mulhyac.lottery.ticket.boot.ticket.domain;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@JacksonXmlRootElement(localName ="message")
public class TicketRequest {

    @JacksonXmlElementWrapper(localName ="orderlist")
    @JacksonXmlProperty(localName ="order")
    private List<OrderRequest> orderList;

    public List<OrderRequest> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<OrderRequest> orderList) {
        this.orderList = orderList;
    }

}

⑵OrderRequest.java

package com.mulhyac.lottery.ticket.boot.ticket.domain;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import javax.xml.bind.annotation.XmlElement;

public class OrderRequest {

    public String lotteryType;

    public String phase;

    @JacksonXmlProperty(localName = "lotterytype")
    public String getLotteryType() {
        return lotteryType;
    }

    public void setLotteryType(String lotteryType) {
        this.lotteryType = lotteryType;
    }

    @JacksonXmlProperty(localName = "phase")
    public String getPhase() {
        return phase;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }
}

⑶TicketResponse.java

package com.mulhyac.lottery.ticket.boot.ticket.domain;


import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.*;

@JacksonXmlRootElement(localName ="message")
public class TicketResponse {

    private List<OrderResponse> orderList;

    @JacksonXmlElementWrapper(localName ="orderlist")
    @JacksonXmlProperty(localName ="order")
    public List<OrderResponse> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<OrderResponse> orderList) {
        this.orderList = orderList;
    }
}

⑷OrderResponse.java

package com.mulhyac.lottery.ticket.boot.ticket.domain;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import javax.xml.bind.annotation.XmlElement;

public class OrderResponse {

    public String orderId;

    public String sysId;

    public String errorCode;

    public String msg;

    public OrderResponse() {
    }

    public OrderResponse(String orderId, String sysId, String errorCode, String msg) {
        this.orderId = orderId;
        this.sysId = sysId;
        this.errorCode = errorCode;
        this.msg = msg;
    }

    @JacksonXmlProperty(localName = "orderid")
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @JacksonXmlProperty(localName = "sysid")
    public String getSysId() {
        return sysId;
    }

    public void setSysId(String sysId) {
        this.sysId = sysId;
    }

    @JacksonXmlProperty(localName = "errorcode")
    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    @JacksonXmlProperty(localName = "msg")
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

5、编写控制器

package com.yilin.lottery.ticket.boot.ticket.controller;

import com.mulhyac.lottery.ticket.boot.ticket.domain.OrderRequest;
import com.mulhyac.lottery.ticket.boot.ticket.domain.OrderResponse;
import com.mulhyac.lottery.ticket.boot.ticket.domain.TicketRequest;
import com.mulhyac.lottery.ticket.boot.ticket.domain.TicketResponse;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SscController {

    @PostMapping(value = "/test", consumes = { MediaType.APPLICATION_XML_VALUE }, produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public TicketResponse test(@RequestBody TicketRequest ticketRequest){
        TicketResponse ticketResponse=new TicketResponse();
        List<OrderResponse> orders=new ArrayList<OrderResponse>();
        OrderResponse o=new OrderResponse();
        o.setMsg("投注成功");
        orders.add(o);
        ticketResponse.setOrderList(orders);
        return ticketResponse;
    }

}

6、使用postman进行测试,选择Body->raw->XML,请求参数如下:

<?xml version="1.0" encoding="utf-8"?>

<message> 
  <orderlist> 
    <order> 
      <lotterytype>2004</lotterytype>  
      <phase>201409209</phase>  
      <orderid>zy2014090234322</orderid>  
      <playtype>200401</playtype>  
      <betcode>00,02,03,04,05,06,07^</betcode>  
      <multiple>1</multiple>  
      <amount>2</amount>  
      <add>0</add>  
      <endtime>结束时间</endtime> 
    </order> 
  </orderlist> 
</message>

响应结果如下:

<message>
    <orderlist>
        <order>
            <orderid/>
            <sysid/>
            <errorcode/>
            <msg>投注成功</msg>
        </order>
    </orderlist>
</message>

这样也就完成了xml的数据请求与响应。

熟练的同学. 也可使用Lombok等优化一下对象!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcybaonier

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值