[spring处理webservice报文] 4 soap报文解析
看这一篇,我希望,你们先把前面的三篇简单过下。避免对标题理解有误差哈。
顺着上一篇的思路,我们来看下rest请求收到soap报文之后如何解析:
注意:下面的代码都是基于前面文章的环境来写的。
解析代码
下面代码里头,我为了调试方便,我在main放里头的直接写了解析的代码:
package com.future.ws.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
@RestController
@RequestMapping("/ws")
public class FutureController {
@PostMapping("/in")
public String wsIn(HttpServletRequest request) {
// 获取报文
String body = getBody(request);
System.out.println(body);
return "";
}
private String getBody(HttpServletRequest request) {
StringBuffer sb = new StringBuffer("");
try (
InputStream in = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
) {
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return sb.toString();
}
}
public static void main(String[] args) throws SOAPException {
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.ws.future.com/\"> <soapenv:Header> \t\t<token>abc</token>\t</soapenv:Header> <soapenv:Body>\t111 </soapenv:Body></soapenv:Envelope>";
SOAPMessage soapMessage = formatSoapString(xml);
SOAPHeader soapHeader = soapMessage.getSOAPHeader();
NodeList token = soapHeader.getElementsByTagName("token");
Node node = token.item(0);
String hToken = node.getTextContent();
System.out.println("======头部:token:");
System.out.println("===" + hToken + "===");
SOAPBody soapBody = soapMessage.getSOAPBody();
System.out.println("======body:");
System.out.println("===" + soapBody.getTextContent().trim().replaceAll("\r|\n|\t","") + "===");
}
/**
* 把soap字符串格式化为SOAPMessage
*
* @param soapString
* @return
* @see [类、类#方法、类#成员]
*/
public static SOAPMessage formatSoapString(String soapString) {
MessageFactory msgFactory;
try {
msgFactory = MessageFactory.newInstance();
SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
new ByteArrayInputStream(soapString.getBytes("UTF-8")));
reqMsg.saveChanges();
return reqMsg;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
解析结果
======头部:token:
===abc===
======body:
===111===
至此,就完成了spring处理soap报文的动作了。
加上这一篇共四篇,一个完整的解决方案。
附录
[spring处理webservice报文] 2 ws服务报文body不能是参数_独行侠_阿涛的博客-CSDN博客目录1 背景2 soap:Body的标签内容不能是参数3 原来人家是rest请求1 背景上一篇,已经讲解了如何在springboot里头搭建webservice服务了,这个这个搭建的方法在spring4.x之后的版本可以不做任何修改直接平行使用。上一篇博客如下:[spring处理webservice报文] 1 spring如何搭建webservice服务_独行侠_阿涛的博客-CSDN博客1 背景最近收到一个需求,比较坑,但是大概的意思看懂了,但是其中让我大意的是:需求说,双方
https://blog.csdn.net/wltsysterm/article/details/124770902 [spring处理webservice报文] 3 rest处理soap报文_独行侠_阿涛的博客-CSDN博客目录1 背景2 rest请求处理soap报文2.1 创建controller3 调试1 背景前面两篇讲解了spring处理soap报文的囧途,如下。这一篇讲解下spring如何通过post类型的请求来处理soap报文。[spring处理webservice报文] 1 spring如何搭建webservice服务_独行侠_阿涛的博客-CSDN博客1 背景最近收到一个需求,比较坑,但是大概的意思看懂了,但是其中让我大意的是:需求说,双方通信采用报文的形式,如下:<soa
https://weilintao.blog.csdn.net/article/details/124771142
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)