Java实现接收xml格式数据并解析,返回xml格式数据

需求描述:后端接受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数据
    }
相关推荐
吾日三省吾码2 小时前
JVM 性能调优
java
弗拉唐3 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi774 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
少说多做3434 小时前
Android 不同情况下使用 runOnUiThread
android·java
知兀4 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员
蓝黑20204 小时前
IntelliJ IDEA常用快捷键
java·ide·intellij-idea
Ysjt | 深4 小时前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++
shuangrenlong5 小时前
slice介绍slice查看器
java·ubuntu
牧竹子5 小时前
对原jar包解压后修改原class文件后重新打包为jar
java·jar