• java 写webservice接口解析xml报文


    1        <!--解析xml报文-->
    2         <dependency>
    3             <groupId>dom4j</groupId>
    4             <artifactId>dom4j</artifactId>
    5             <version>1.6.1</version>
    6         </dependency>    

    新增一个接口

    1 @WebService
    2 public interface IAPP_WS_002_insertUserDateService {
    3 
    4    public void insertUserDate(String body);
    5 
    6 }

    实现类

     1 import com.webservice.server.service.IAPP_WS_002_insertUserDateService;
     2 import org.dom4j.Document;
     3 import org.dom4j.DocumentException;
     4 import org.dom4j.DocumentHelper;
     5 import org.dom4j.Element;
     6 
     7 import javax.jws.WebService;
     8 import java.util.Iterator;
     9 
    10 /**
    11  * 插入人员数据
    12  */
    13 @SuppressWarnings("all")
    14 @WebService
    15 public class APP_WS_002_insertUserDateServiceImpl implements IAPP_WS_002_insertUserDateService {
    16 
    17     /**
    18      * 解析传来的xml报文
    19      *
    20      * @param body
    21      */
    22     @Override
    23     public void insertUserDate(String body) {
    24         readStringXml(body);
    25     }
    26 
    27 
    28     /**
    29      * 解析传来的xml报文
    30      *
    31      * @param xml
    32      */
    33     public static void readStringXml(String xml) {
    34         Document doc = null;
    35         try {
    36             doc = DocumentHelper.parseText(xml); // 将字符串转为XML
    37             Element rootElt = doc.getRootElement(); // 获取根节点
    38             System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称 DATA
    39             Iterator iter = rootElt.elementIterator("HEAD"); // 获取根节点下的子节点HEAD
    40             // 遍历HEAD节点
    41             while (iter.hasNext()) {
    42                 Element recordEle = (Element) iter.next();
    43                 String BIZTRANSACTIONID = recordEle.elementTextTrim("BIZTRANSACTIONID"); //取HEAD下节点的值
    44                 String COUNT = recordEle.elementTextTrim("COUNT"); //取HEAD下节点的值
    45                 String CONSUMER = recordEle.elementTextTrim("CONSUMER"); //取HEAD下节点的值
    46                 String SRVLEVEL = recordEle.elementTextTrim("SRVLEVEL"); //取HEAD下节点的值
    47                 String ACCOUNT = recordEle.elementTextTrim("ACCOUNT"); //取HEAD下节点的值
    48                 String PASSWORD = recordEle.elementTextTrim("PASSWORD"); //取HEAD下节点的值
    49 
    50                 System.out.println("BIZTRANSACTIONID:" + BIZTRANSACTIONID);
    51                 System.out.println("COUNT:" + COUNT);
    52                 System.out.println("CONSUMER:" + CONSUMER);
    53                 System.out.println("SRVLEVEL:" + SRVLEVEL);
    54                 System.out.println("ACCOUNT:" + ACCOUNT);
    55                 System.out.println("PASSWORD:" + PASSWORD);
    56             }
    57             Iterator iterss = rootElt.elementIterator("LIST"); ///获取根节点下的子节点LIST
    58             // 遍历LIST节点
    59             while (iterss.hasNext()) {
    60                 Element recordEless = (Element) iterss.next();
    61                 Iterator itersElIterator = recordEless.elementIterator("ITEM"); // 获取子节点LIST下的子节点ITEM
    62                 // 遍历LIST节点下的ITEM节点
    63                 while (itersElIterator.hasNext()) {
    64                     Element itemEle = (Element) itersElIterator.next();
    65                     String ID = itemEle.elementTextTrim("ID");
    66                     String NAME = itemEle.elementTextTrim("NAME");
    67                     String ADDR = itemEle.elementTextTrim("ADDR");
    68                     String SEX = itemEle.elementTextTrim("SEX");
    69                     String AGE = itemEle.elementTextTrim("AGE");
    70                     String MARK = itemEle.elementTextTrim("MARK");
    71 
    72                     System.out.println("ID:" + ID);
    73                     System.out.println("NAME:" + NAME);
    74                     System.out.println("ADDR:" + ADDR);
    75                     System.out.println("SEX:" + SEX);
    76                     System.out.println("AGE:" + AGE);
    77                     System.out.println("MARK:" + MARK);
    78                 }
    79             }
    80         } catch (DocumentException e) {
    81             e.printStackTrace();
    82         } catch (Exception e) {
    83             e.printStackTrace();
    84         }
    85     }
    86 }

    发布

     1 import com.webservice.server.service.impl.APP_WS_001getNoteByIdServiceImpl;
     2 import com.webservice.server.service.impl.APP_WS_002_insertUserDateServiceImpl;
     3 import org.springframework.boot.ApplicationArguments;
     4 import org.springframework.boot.ApplicationRunner;
     5 import org.springframework.stereotype.Component;
     6 import javax.xml.ws.Endpoint;
     7 
     8 @Component
     9 @SuppressWarnings("all")
    10 public class WebserviceConfig implements ApplicationRunner {
    11 
    12    
    13     private String add_WS_002 = "http://localhost:8081/webservice/insertUserDate";
    14 
    15     @Override
    16     public void run(ApplicationArguments args) throws Exception {
    17         Endpoint.publish(add_WS_002, new APP_WS_002_insertUserDateServiceImpl());
    18         System.out.println("webservice接口 发布成功");
    19     }
    20 }

    测试

    使用SOAPUI测试

    后台获取如下

     

    原创文章,转载请说明出处,谢谢合作
  • 相关阅读:
    Redis分布式锁的正确加锁与解锁方式
    高并发系统中的限流算法
    谈谈 InnoDB引擎中的一些索引策略
    面试必问:手写一个内存泄漏的程序
    空指针异常与Optional类
    Redis的内存淘汰策略
    explain的关键字段的意义
    pycharm编辑器简单配置-- 持续更新完善
    vscode 插件-better comments-代码注释高亮
    阿里前端开发规范-转载
  • 原文地址:https://www.cnblogs.com/lwl80/p/14947816.html
Copyright © 2020-2023  润新知