SpringBoot 接收xml数据

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



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    

    
        UTF-8
        UTF-8
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        
    


    
        
                
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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依赖


    com.fasterxml.jackson.jaxrs
    jackson-jaxrs-xml-provider

上述没使用,使用的依赖是下方的,不知何解。。


    com.fasterxml.jackson.dataformat
    jackson-dataformat-xml
    2.8.8

 

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 orderList;

    public List getOrderList() {
        return orderList;
    }

    public void setOrderList(List 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 orderList;

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

    public void setOrderList(List 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 orders=new ArrayList();
        OrderResponse o=new OrderResponse();
        o.setMsg("投注成功");
        orders.add(o);
        ticketResponse.setOrderList(orders);
        return ticketResponse;
    }

}

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



 
   
     
      2004  
      201409209  
      zy2014090234322  
      200401  
      00,02,03,04,05,06,07^  
      1  
      2  
      0  
      结束时间 
     
   

响应结果如下:


    
        
            
            
            
            投注成功
        
    

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

本文版权归https://www.mulhyac.com所有,转载请注明出处.

网址:https://www.mulhyac.com

你可能感兴趣的:(java)