提示:本文显示baidu.com,皆为脱敏用,并非真正地址。
目录
大家WebService接口还在使用生成的类去实现调用的吗?
其实Hutool的WebService工具很厉害,但是文档写的不太清楚,或者示例不够多,我主要做医疗业务,对接方基本都是WebService接口,刚开始使用hutool的时候确实花费了挺长时间研究,最终失败告终,奈何业务多,有丰富的机会去尝试,现在基本啥格式的都可以克服了。
Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。
拿到三方提供的链接后,需要进行验证下
例如:第三方提供链接:http://localhost:8080/XXX/services/xxxxxxImpl
需要改成:第三方提供链接:http://localhost:8080/XXX/services/xxxxxxImpl?wsdl
然后用浏览器打开能像下图展示即可:
使用SoapUI进行接口调用一次(没有SoapUI的自己去安装下)
如下图进行创建



打开,选择要调用的接口,点开request后,后边会出现一个框,里面有调用入参和请求地址
<![CDATA[<request><type>1</type></request>]]>
针对上面的数据,我这边写好工具类,先上干货,然后一一为大家讲解:
package com.ceshi;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.webservice.SoapClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* @author 饭团
* @version 1.0
* @description: TODO
* @date 2022/10/24 14:14
*/
public class a {
private static Logger log = LoggerFactory.getLogger(a.class);
public static void main(String[] args) {
StringBuilder params = new StringBuilder("<![CDATA[<request>");
params.append("<type>0</type></request>]]>");
request("ReceiveDataAll",params.toString());
}
/**
* @description: 请求方法
* @param: method 请求方法
* @param: params 入参字符串
* @return: java.util.Map<java.lang.String,java.lang.Object>
* @author 周培武
* @date: 2022/10/24 14:23
*/
private static Map<String, Object> request(String method, String params){
String result = "";
String requestId = UUID.randomUUID().toString();
try{
String url = "http://localhost:8080/xxxxxx/services/xxxxServiceImpl";
SoapClient client = SoapClient.create(url)
.header("SOAPAction","")
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("ser:"+method, "http://service.mocire.baidu.com/")
.setParam("message",params,false);//此处写true,会自动填写命名空间
log.info("[请求ID:{}]CSDN测试方法字符串接口:{},入参:{}",requestId,url,client.getMsgStr(true));
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
result = client.send(false);
log.info("[请求ID:{}]CSDN测试方法字符串出参:{}",requestId,result);
if(result.contains("<![CDATA[")){
result = result.substring(result.indexOf("<![CDATA[")+9,result.indexOf("]]>"));
}else{
result = result.substring(result.indexOf("Result>")+7,result.indexOf("}</")+1);
}
Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
return resultMap;
}catch (Exception ex){
log.error("[请求ID:{}]CSDN测试方法字符串出现异常,入参:{},出参:{},{}",requestId,params,result,ex);
throw ex;
}
}
}
如果是多个入参,可以选用setParams,写入一个map,键值对,然后统一配置是否添加前缀

我这边只是拿这个接口做了一个测试,下面会给大家整理下我这边遇到的各种不通类型的请求信息和工具类
请求数据:
接口地址:http://localhost:8080/xxxxxx/services/xxxxServiceImpl
请求头:SOAPAction: ""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.mocire.baidu.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:ReceiveDataAll>
<!--Optional:-->
<message><![CDATA[<request><type>1</type></request>]]></message>
</ser:ReceiveDataAll>
</soapenv:Body>
</soapenv:Envelope>
请求方法:
public static void main(String[] args) {
StringBuilder params = new StringBuilder("<![CDATA[<request>");
params.append("<type>0</type></request>]]>");
request("ReceiveDataAll",params.toString());
}
/**
* @description: 请求方法
* @param: method 请求方法
* @param: params 入参字符串
* @return: java.util.Map<java.lang.String,java.lang.Object>
* @author 周培武
* @date: 2022/10/24 14:23
*/
private static Map<String, Object> request(String method, String params){
String result = "";
String requestId = UUID.randomUUID().toString();
try{
String url = "http://localhost:8080/xxxxxx/services/xxxxServiceImpl";
SoapClient client = SoapClient.create(url)
.header("SOAPAction","")
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("ser:"+method, "http://service.mocire.baidu.com/")
.setParam("message",params,false);//此处写true,会自动填写命名空间
log.info("[请求ID:{}]CSDN测试方法字符串接口:{},入参:{}",requestId,url,client.getMsgStr(true));
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
result = client.send(false);
log.info("[请求ID:{}]CSDN测试方法字符串出参:{}",requestId,result);
if(result.contains("<![CDATA[")){
result = result.substring(result.indexOf("<![CDATA[")+9,result.indexOf("]]>"));
}else{
result = result.substring(result.indexOf("Result>")+7,result.indexOf("}</")+1);
}
Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
return resultMap;
}catch (Exception ex){
log.error("[请求ID:{}]CSDN测试方法字符串出现异常,入参:{},出参:{},{}",requestId,params,result,ex);
throw ex;
}
}
请求数据:
接口地址:http://localhost:8080/xxxxxx/services/xxxxServiceImpl
请求头:SOAPAction: "http://tempuri.org/GetThirdAppData"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetThirdAppData>
<tem:strPatientId>1000001</tem:strPatientId>
<tem:strHospitalNo>1</tem:strHospitalNo>
<tem:strXML><![CDATA[<HOSPITAL_NO>1</HOSPITAL_NO><PATIENT_ID>1000001</PATIENT_ID>]]></tem:strXML>
</tem:GetThirdAppData>
</soapenv:Body>
</soapenv:Envelope>
请求示例:
public static Map<String, Object> write(String param){
String result = "";
String requestId = UUID.randomUUID().toString();
try{
String hospitalNo = "1";
String patientId = "1000001";
//拼接strXML
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("HOSPITAL_NO", hospitalNo);//医院编码
paramMap.put("PATIENT_ID",patientId);//患者ID
String paramStr = "<![CDATA["+XmlUtil.mapToXmlStr(paramMap)+"]]>";
//拼接外层入参
Map<String,Object> params = new HashMap<>();
params.put("strPatientId",patientId);
params.put("strHospitalNo",hospitalNo);
params.put("strXML",paramStr);
//新建客户端
String url = "";
SoapClient client = SoapClient.create("http://localhost:8080/xxxxxx/services/xxxxServiceImpl")
.header("SOAPAction","http://tempuri.org/GetThirdAppData")
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("tem:GetThirdAppData", "http://tempuri.org/")
.setParams(params,true);
log.info("[请求ID:{}]CSDN测试方法字符串接口:{},入参:{}",requestId,url,client.getMsgStr(true));
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
result = client.send(false);
log.info("[请求ID:{}]CSDN测试方法字符串出参:{}",requestId,result);
if(result.contains("<![CDATA[")){
result = result.substring(result.indexOf("<![CDATA[")+9,result.indexOf("]]>"));
}else{
result = result.substring(result.indexOf("Result>")+7,result.indexOf("}</")+1);
}
Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
return resultMap;
}catch (Exception ex){
log.error("[请求ID:{}]CSDN测试方法字符串出现异常出参:{},{}",requestId,result,ex);
throw ex;
}
}
请求数据:
接口地址:http://localhost:8080/xxxxxx/services/xxxxServiceImpl.cls?CfgItem=A101查询患者信息接口
请求头:SOAPAction: "http://baidu.com/BS.XmlService.Send"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bjg="http://baidu.com">
<soapenv:Header/>
<soapenv:Body>
<bjg:Send>
<bjg:pInput><![CDATA[<request><idNo>0</type></idNo>]]></bjg:pInput>
</bjg:Send>
</soapenv:Body>
</soapenv:Envelope>
请求示例:
public static void main(String[] args) {
StringBuilder params = new StringBuilder("<![CDATA[<request>");
params.append("<idNo>0</idNo></request>]]>");
request("A101查询患者信息接口",params.toString());
}
/**
* @description: 请求方法
* @param: method 请求方法
* @param: params 入参字符串
* @return: java.util.Map<java.lang.String,java.lang.Object>
* @author 周培武
* @date: 2022/10/24 14:23
*/
private static Map<String, Object> request(String method, String params){
String result = "";
String requestId = UUID.randomUUID().toString();
try{
String url = "http://localhost:8080/xxxxxx/services/xxxxServiceImpl.cls?CfgItem="+method;
SoapClient client = SoapClient.create(url)
.header("SOAPAction","http://baidu.com/BS.XmlService.Send")
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("bjg:Send", "http://baidu.com")
.setParam("pInput",params,true);
log.info("[请求ID:{}]CSDN请求方法字符串接口:{},入参:{}",requestId,url,client.getMsgStr(true));
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
result = client.send(false);
log.info("[请求ID:{}]CSDN请求方法字符串出参:{}",requestId,result);
if(result.contains("<![CDATA[")){
result = result.substring(result.indexOf("<![CDATA[")+9,result.indexOf("]]>"));
}else{
result = result.substring(result.indexOf("Result>")+7,result.indexOf("}</")+1);
}
Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
return resultMap;
}catch (Exception ex){
log.error("[请求ID:{}]CSDN请求方法字符串出现异常,接口:{},入参:{},出参:{},{}",requestId,method,params,result,ex);
throw ex;
}
}
请求数据:
接口地址:http://localhost:8080/xxxxxx/services/xxxxServiceImpl.cls
请求头:SOAPAction: "http://tempuri.org/ITJTCService/SubmitReservationExam"
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ly="http://schemas.datacontract.org/2004/07/LY.WebService.Service.Models">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<tem:SubmitReservationExam xmlns:tem="http://tempuri.org/">
<tem:info>
<ly:CardNo>111111111111</ly:CardNo>
</tem:info>
</tem:SubmitReservationExam>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
请求示例:
private static Map<String, Object> request(String params) throws SOAPException {
String result = "";
String requestId = UUID.randomUUID().toString();
try{
//新建客户端
String url = "http://localhost:8080/xxxxxx/services/xxxxServiceImpl";
SoapClient client = SoapClient.create(url)
.header("SOAPAction","http://tempuri.org/ITJTCService/SubmitReservationExam")
// 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
.setMethod("tem:SubmitReservationExam","http://tempuri.org/");
//修改一级和二级前缀为tem
SOAPBodyElement soapBodyElement = client.getMethodEle();
soapBodyElement.getParentElement().getParentElement().addNamespaceDeclaration("tem","http://tempuri.org/").addNamespaceDeclaration("ly","http://schemas.datacontract.org/2004/07/LY.WebService.Service.Models");
SOAPElement soapElement = soapBodyElement.addChildElement(new QName("http://tempuri.org/","info","tem"));
soapElement.addChildElement("CardNo","ly").setValue("111111111111111111");
log.info("[请求ID:{}]CSDN测试方法字符串接口:{},入参:{}",requestId,url,client.getMsgStr(true));
// 发送请求,参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串,可以配合XmlUtil解析这个响应
result = client.send(false);
log.info("[请求ID:{}]CSDN测试方法字符串出参:{}",requestId,result);
if(result.contains("<![CDATA[")){
result = result.substring(result.indexOf("<![CDATA[")+9,result.indexOf("]]>"));
}else{
result = result.substring(result.indexOf("Result>")+7,result.indexOf("}</")+1);
}
Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
return resultMap;
}catch (Exception ex){
log.error("[请求ID:{}]CSDN请求方法字符串出现异常,入参:{},出参:{},{}",requestId,params,result);
throw ex;
}
}
此入参类型方法特别难调用,之前反馈过hutool,hutool暂不打算深入优化,我这边提供的解决方案hutool推荐使用。

其实大部分入参类型都包含在这里面了,如果还有新的,无法使用的可以将wsdl页面右键查看源文件把html代码发给我,我这边研究下,有啥问题也欢迎在下面提问,我这边看见就会回答的。
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po