目录
1 背景
2 rest请求处理soap报文
2.1 创建controller
3 调试
1 背景
前面两篇讲解了spring处理soap报文的囧途,如下。这一篇讲解下spring如何通过post类型的请求来处理soap报文。
[spring处理webservice报文] 1 spring如何搭建webservice服务_独行侠_阿涛的博客-CSDN博客1 背景最近收到一个需求,比较坑,但是大概的意思看懂了,但是其中让我大意的是:需求说,双方通信采用报文的形式,如下:<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ws.future.com/"> <soap:Header/> <soap:Body> 报文密文 </soap:Body&g...https://blog.csdn.net/wltsysterm/article/details/124765874
2 rest请求处理soap报文
soap报文格式在前面两篇文章都有了,这边同样的,再次复制一遍:
需求说,双方通信采用报文的形式,如下:
<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ws.future.com/">
<soap:Header/>
<soap:Body>
报文密文
</soap:Body>
</soap:Envelope>
2.1 创建controller
package com.future.ws.controller;
import ch.qos.logback.core.net.SyslogOutputStream;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
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();
}
}
}
3 调试
现在我们任何使用soapui模拟rest的post类型请求:
点击调试按钮,查看服务端打印出来的日志,可以看到能够拿到全部的报文信息:
2022-05-14 17:29:15.402 INFO 5788 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 18 ms
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ws.future.com/"> <soapenv:Header/> <soapenv:Body> 111 </soapenv:Body></soapenv:Envelope>
因此,显然我们的分析是对。下面一篇,分享下如何解析soap报文