spring boot 接口 XML 接受和请求(被调方)

spring boot 接口处理:

import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.xianjinxia.sms.pojo.output.BaseResponse;
import com.xianjinxia.sms.service.impl.cloudfeng.sms.util.CloudFengUtil;
import com.xianjinxia.sms.service.impl.cloudfeng.sms.xmlutil.Body;
import com.xianjinxia.sms.service.impl.cloudfeng.sms.xmlutil.Record;
import com.xianjinxia.sms.service.impl.cloudfeng.sms.xmlutil.Records;
import com.xianjinxia.sms.service.impl.cloudfeng.sms.xmlutil.XmlRemoteDto;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AA {
	/**
	 * yunfeng POST 方式  用的是@RequestBody  注解 参数类型是String
	 * @param req
	 * @return
	 */
	@PostMapping("/rpt/smsStatusPullYunFeng")
	public BaseResponse smsStatusPullYunFeng(@RequestBody String msgreport) {
		BaseResponse resp = new BaseResponse();
		log.info("yunfeng,回调参数为={}",msgreport);
		XmlRemoteDto dto = (XmlRemoteDto) CloudFengUtil.convertXmlStrToObject(XmlRemoteDto.class, msgreport);
		Body body = dto.getBody();
		Records records = body.getRecords();
		List rec = records.getRecord();
		Record record = rec.get(0);
			int count = 0;
			// ...............
		return resp;
	}
}

xml 格式的字符串 转成 Java 对象的 工具类:


import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class CloudFengUtil {
	/**
     * 解析内容
     *
     * @param clazz
     * @param xmlStr
     * @return
     */
    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口  
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
}

XML 需要转换的几个JavaBean:

XmlRemoteDto:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
最重要的就是 不要 XmlType  和 XmlRootElement 注解不要弄混了
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"head","body"})
@XmlRootElement(name="xml")
public class XmlRemoteDto {
	private Head head;
	private Body body;

//  setter  getter  方法	

// toString 方法
	@Override
	public String toString() {
		return "XmlRemoteDto [head=" + head + ", body=" + body + "]";
	}
}

Head:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="head")    // 当前节点名称
public class Head {
	@XmlElement(name = "app_key")      // 子节点名称
	private String appKey;
	@XmlElement(name = "time_stamp")    // 子节点名称
	private String timeStamp;
	@XmlElement(name = "nonce_str")      // 子节点名称
	private String nonceStr;
	@XmlElement(name = "sign")     // 子节点名称
	private String sign;

// setter getter 方法

// toString 方法
	@Override
	public String toString() {
		return "Head [appKey=" + appKey + ", timeStamp=" + timeStamp + ", nonceStr=" + nonceStr + ", sign=" + sign
				+ "]";
	}
}

Body :

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"records"})	// 子节点
@XmlRootElement(name="body")   // 当前节点  
public class Body {
	@XmlElement(name = "records")    // 子节点名称
	private Records records;
	
	// setter  getter 方法 
	// toString 方法
	@Override
	public String toString() {
		return "Body [records=" + records + "]";
	}
}

Records: 

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"record"})
@XmlRootElement(name="records")
public class Records {

	private List record;
	// setter  getter 方法 
	
	//toString方法
	@Override
	public String toString() {
		return "Records [record=" + record + "]";
	}
}

Record : 


import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="record")
public class Record {

	@XmlElement(name = "mission_num")
	private String missionNum;
	@XmlElement(name = "dest_id")
	private String destId;
	
	@XmlElement(name = "send_status")
	private String sendStatus;
	@XmlElement(name = "receive_status")
	private String receiveStatus;
	
	@XmlElement(name = "batch_num")
	private String batchNum;
	@XmlElement(name = "stat_time")
	private String statTime;
	// setter  getter 方法 
	
	//toString方法
	@Override
	public String toString() {
		return "Record [missionNum=" + missionNum + ", destId=" + destId + ", sendStatus=" + sendStatus
				+ ", receiveStatus=" + receiveStatus + ", batchNum=" + batchNum + ", statTime=" + statTime + "]";
	}
}

如果接口参数 这种类型 我们请求处理如下:

spring boot 接口 XML 接受和请求(被调方)_第1张图片

上图中的测试的xml 原文件:




  
    mdDve0aYHGvSNZlcke0QyA3kSZihXYdi
    20181106175812
    go9d1cjk644d7wuaa7v8g7wkijh00ie2
    f716b0f0463f3df7356c473da37ff449
  
  
    
      
        verify_code20181106175707
        13272661024
        FAIL
        FAIL
        DB:9141
        shilukj0120181106175707
        2018-11-06 17:58:12
      
    
  

 

你可能感兴趣的:(spring,boot)