先看WSDL报文(访问wsdl地址):如下 是服务端发布的服务
http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.CLS?WSDL
根据wsdl报文生成客户端代码
- 优点:可以像调本地接口一样访问;
- 缺点:服务端接口发生改变代码得重新生成;
将此网页另存为 以.xml 保存在本地
将<s:element ref="s:schema" /> 全部删除掉不然在生成java文件的时候会报错,没有则忽略
执行 wsimport -keep *.xml 命令其实还有一个生成命令 wsimport -keep url?wsdl但是由于要改变一些xml文件里的内容所以
生成如下客户端代码:
将生成的代码拷贝到项目中,注:放在java根目录下,最好不要擅自修改路径,之前踩到一个坑,修改了后,调用失败
准备工作就绪后,编写测试类:
package org.controller;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tempuri.PUB0015;
import org.tempuri.PUB0015Soap;
@RestController
@RequestMapping(value="/hello")
public class test {
@GetMapping("/getWebServiceResult")
public String getService() throws Exception {
String url = "http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.cls?wsdl"; // WebService服务地址
String data="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <tem:HIPMessageServer>\n" +
" <!--Optional:-->\n" +
" \n" +
" <!--Optional:-->\n" +
" <tem:input1>MES0045</tem:input1><tem:input2><![CDATA["+"<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>"+"]]></tem:input2>\n" +
" </tem:HIPMessageServer>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
String date2 = "<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000).setSocketTimeout(6000).build();
httpPost.setConfig(requestConfig);
StringEntity entity = new StringEntity(data, "UTF-8");
httpPost.setEntity(entity);
CloseableHttpResponse httpResponse = null;
//back为服务端返回的原始soap格式的xml数据,并且实际有用数据的“<”被转译成了“<”,需要自行处理
// String back=getResult(httpResponse, httpClient, httpPost);
httpResponse.close();
httpClient.close();
return "back";
}
public static String pub0015SoapClien(String input1, String input2){
PUB0015 pub0015 = new PUB0015();
PUB0015Soap PUB0015Soap = pub0015.getPUB0015Soap();
String result = PUB0015Soap.hipManagerService(input1,input2);
System.out.println("-------result结果----------"+result);
return result;
}
public static void main(String args[]){
String input1 = "MES0045";
String input2 = "<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>";
pub0015SoapClien(input1,input2);
}
public static String getClientDataXML(){
StringBuilder soapBuilder = new StringBuilder(64);
soapBuilder.append("<Request>");
soapBuilder.append(" <Header>");
soapBuilder.append(" <SourceSystem></SourceSystem>");
soapBuilder.append(" <MessageID></MessageID>");
soapBuilder.append(" </Header>");
soapBuilder.append(" <Body>");
soapBuilder.append(" <Content>");
soapBuilder.append(" <Context>"+"999999"+"</Context>");
soapBuilder.append(" <ActionTypeCode>"+"888955"+"</ActionTypeCode>");
soapBuilder.append(" <FromUserRowId>"+"888955"+"</FromUserRowId>");
soapBuilder.append(" <EpisodeId></EpisodeId>");
soapBuilder.append(" <OrdItemId></OrdItemId>");
soapBuilder.append(" <ToUserRowId>"+"888955"+"</ToUserRowId>");
soapBuilder.append(" <OtherInfoJson> {\"\"link\"\":\"\""+"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1"+"\"\"}</OtherInfoJson>");
soapBuilder.append(" <ToLocRowId></ToLocRowId>");
soapBuilder.append(" <EffectiveDays></EffectiveDays>");
soapBuilder.append(" <CreateLoc></CreateLoc>");
soapBuilder.append(" </Content>");
soapBuilder.append(" </Body>");
soapBuilder.append("</Request>");
return soapBuilder.toString();
}
}
测试结果如下:
pom 导入jar 如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
自此根据wsdl生成客户端代码完成,最近做医疗方面的项目,整webservice接口实属无奈,哎
注意:
-> 发现本地生成的 客户端 wsdlLocation 指向的是 本地路径
-> 修改为服务端地址 WSDL
- "http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.CLS?WSDL"