需求描述:后端接受xml格式数据,解析出相应数据,并返回xml格式数据。
xml
<!--XML解析-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
java
package com.zlp.cps.dto;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
/**
* 接收xml格式数据解析类
*
* @author zlp
* @date 2023/8/25
**/
@Data
@XStreamAlias("xml")
public class WxUserMessageModel {
@XStreamAlias("ToUserName")
private String toUserName;
@XStreamAlias("FromUserName")
private String fromUserName;
@XStreamAlias("CreateTime")
private Long createTime;
@XStreamAlias("MsgType")
private String msgType;
@XStreamAlias("Content")
private String content;
@XStreamAlias("MsgId")
private String msgId;
@XStreamAlias("Title")
private String title;
@XStreamAlias("Description")
private String description;
@XStreamAlias("Url")
private String url;
/**
* 订阅或者取消订阅的事件
*/
@XStreamAlias("Event")
private String event;
@XStreamAlias("EventKey")
private String eventkey;
}
java
public String acceptMsg(HttpServletRequest request, ServletResponse response) {
// 数据提取
StringBuffer reqXmlData = new StringBuffer();
try {
InputStream inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while ((s = in.readLine()) != null) {
reqXmlData.append(s);
}
in.close();
inputStream.close();
} catch (IOException e) {
System.out.println("流解析xml数据异常!");
e.printStackTrace();
}
//判断请求数据是否为空
if (reqXmlData.length() <= 0) {
System.out.println("请求数据为空!");
}
//json类型数据
JSONObject jsonObject = XML.toJSONObject(reqXmlData.toString());
log.info("xml :{}", jsonObject.toString());
Object obj = jsonObject.get("xml");
JSONObject data = JSONUtil.parseObj(obj);
String fromUserName = data.get("FromUserName").toString();
String toUser = data.get("ToUserName").toString();
String context = data.get("Content").toString();
// 返回数据
WxUserMessageModel responseXmlData = new WxUserMessageModel();
responseXmlData.setToUserName(fromUserName);
responseXmlData.setFromUserName(toUser);
responseXmlData.setCreateTime(System.currentTimeMillis());
responseXmlData.setMsgType("text");
// 按需编写业务
if (context.startsWith(GzhConstant.tbSearch)) {
String[] split = context.split("\\+");
context = split[1];
long pageNum = 1L;
if (split.length > 2) {
pageNum = Long.parseLong(split[2]);
}
// 调用淘宝商品搜索服务
TbCpsReq req = new TbCpsReq();
req.setKeyword(context);
req.setPageNum(pageNum);
req.setPageSize(8L);
BaseResponse search = tbCpsService.search(req);
List<TbMaterialData> list = (List<TbMaterialData>) search.getData();
if (ListUtil.isEmpty(list)) {
context = "该商品暂时没有优惠券";
} else {
context = convertSearchToMsg(list);
}
} else if (context.startsWith(GzhConstant.pddSearch)) {
context = "拼多多搜索服务升级中!!!";
} else if (context.startsWith(GzhConstant.jdSearch)) {
context = "京东搜索服务升级中!!!";
} else {
return GzhConstant.defaultReply;
}
responseXmlData.setContent(context);
XStream xstream = new XStream();
xstream.processAnnotations(WxUserMessageModel.class);
xstream.setClassLoader(WxUserMessageModel.class.getClassLoader());
log.info("return xml : {}", xstream.toXML(responseXmlData));
return xstream.toXML(responseXmlData); //XStream的方法,直接将对象转换成 xml数据
}