Java-代码段-解析xml入参-并返回xml格式的数据
1、postman POST 入参 :
body->raw->XML
<?xml version="1.0" encoding="utf-8"?>
<requests>
<request>
<COMMAND>SUPPLIER_ADD</COMMAND>
<DIS_ID>10106262</DIS_ID>
<CODE>30027317</CODE>
<ORG_CODE>HG1101</ORG_CODE>
<FULL_NAME>大连住友流体设备有限公司</FULL_NAME>
<SOCIAL_CREDIT_CODE>91210211MA0UQKPQ2T</SOCIAL_CREDIT_CODE>
<COUNTRY>中国</COUNTRY>
<PROVINCE>辽宁</PROVINCE>
<CITY>大连市</CITY>
<ADDRESS>辽宁省大连市甘井子区玉峰街24号1单元2层1号 </ADDRESS>
<OPR_TYPE>TJ</OPR_TYPE>
<DATASTATUS>启用</DATASTATUS>
<IS_CUSTOMER>0</IS_CUSTOMER>
<IS_SUPPLIER>1</IS_SUPPLIER>
<UNIONCODELST>HG1101</UNIONCODELST>
</request>
<request>
<COMMAND>SUPPLIER_ADD2</COMMAND>
<DIS_ID>101062622</DIS_ID>
<CODE>300273172</CODE>
<ORG_CODE>HG11012</ORG_CODE>
<FULL_NAME>大连住友流体设备有限公司2</FULL_NAME>
<SOCIAL_CREDIT_CODE>91210211MA0UQKPQ2T2</SOCIAL_CREDIT_CODE>
<COUNTRY>中国2</COUNTRY>
<PROVINCE>辽宁2</PROVINCE>
<CITY>大连市2</CITY>
<ADDRESS>辽宁省大连市甘井子区玉峰街24号1单元2层1号2 </ADDRESS>
<OPR_TYPE>TJ</OPR_TYPE>
<DATASTATUS>启用</DATASTATUS>
<IS_CUSTOMER>0</IS_CUSTOMER>
<IS_SUPPLIER>1</IS_SUPPLIER>
<UNIONCODELST>HG1101</UNIONCODELST>
</request>
</requests>
返回:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<result>
<code>200</code>
<message>成功:大连市</message>
</result>
package com.safety51.proj.syncOrg.controller;
import com.safety51.bootstrap.commons.annotation.IgnoreResponseWrapper;
import com.safety51.proj.syncMdm.pojo.XmlVo;
import com.safety51.proj.syncMdm.service.SyncMDM2XgfSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.activation.MimetypesFileTypeMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
@RequestMapping("/api/syncMdm2Xgf")
@RestController
public class SyncMDM2XgfController {
@Autowired
private SyncMDM2XgfService syncMDM2XgfService;
@PostMapping("/insertOrUpdate")
//这里可能需要返回xml
@ResponseBody
@IgnoreResponseWrapper //这个在按原则这边必须有,不然会被底层保证成json
public ResponseEntity<String> insertOrUpdate(@RequestBody String body) {
XmlVo xmlVo = syncMDM2XgfService.insertOrUpdate(body);
HttpHeaders headers = new HttpHeaders();
// headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
// headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(modalName + ".xls"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
String xml = convertToXML(xmlVo);
return ResponseEntity
.ok()
.headers(headers)
// .contentLength(xml != null ? xml.length() : 0) 这个设置的不对的话会截断数据
//.contentType(MediaType.parseMediaType(getContentType(path)))
.contentType(MediaType.parseMediaType("application/xml"))
//.body(new FileSystemResource(file));
.body(xml);
}
public static <T> String convertToXML(T t) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(t.getClass());
Marshaller marshaller = null;
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(t, writer);
return writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
private static String getContentType(String filePath) {
//利用nio提供的类判断文件ContentType
Path path = Paths.get(filePath);
String content_type = null;
try {
content_type = Files.probeContentType(path);
} catch (IOException e) {
//log.error("Read File ContentType Error");
}
//若失败则调用另一个方法进行判断
if (content_type == null) {
content_type = new MimetypesFileTypeMap().getContentType(filePath);
}
return content_type;
}
}
package com.safety51.proj.syncMdm.service.impl;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.google.common.base.Preconditions;
import com.safety51.bootstrap.commons.exception.BizzException;
import com.safety51.proj.syncMdm.pojo.XmlVo;
import com.safety51.proj.syncMdm.service.SyncMDM2XgfSevice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Service
@Slf4j
public class SyncMDM2XgfServiceImpl extends BaseService implements SyncMDM2XgfService {
@Override
public XmlVo insertOrUpdate(String body) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
HttpServletRequest request = requestAttributes.getRequest();
String token = request.getHeader("token");
}
//todo 返回xml 文件上传
// import cn.hutool.json.JSONObject;
// import cn.hutool.json.XML;
Preconditions.checkArgument(StringUtils.isNotBlank(body), "xml入参不能为空");
JSONObject jsonObject = XML.toJSONObject(body);
if (jsonObject == null) {
throw new BizzException("解析xml失败");
}
JSONObject requests = jsonObject.getJSONObject("requests");
JSONArray request = requests.getJSONArray("request");
Object o = request.get(0);
//必须要带@XmlRootElement注解的实体
XmlVo result = new XmlVo();
if (o instanceof JSONObject) {
String city = ((JSONObject) request.get(0)).getStr("CITY", "");
result.setCode(200);
result.setMessage("成功:"+city);
return result;
}
result.setCode(201);
result.setMessage("失败");
return result;
}
}
正文到此结束
- 本文标签: Spring Boot Spring
- 本文链接: https://code.jiangjiesheng.cn/article/224
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。