前言
调用webservice接口的方式有很多,今天记录一下,使用 spring web services 调用 soap webservice接口
一.导入依赖
<!-- spring boot web依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!-- spring web services -->
<dependency>
<groupid>org.springframework.ws</groupid>
<artifactid>spring-ws-core</artifactid>
</dependency>
<!-- apache httpclient 作为 webservice 客户端 -->
<dependency>
<groupid>org.apache.httpcomponents</groupid>
<artifactid>httpclient</artifactid>
</dependency>
<!-- jaxb api -->
<dependency>
<groupid>javax.xml.bind</groupid>
<artifactid>jaxb-api</artifactid>
<version>2.3.1</version>
</dependency>
<!-- jaxb 运行时 -->
<dependency>
<groupid>org.glassfish.jaxb</groupid>
<artifactid>jaxb-runtime</artifactid>
<version>2.3.5</version>
</dependency>二.创建请求类和响应类
根据soap的示例,创建请求类和响应类
soap示例
请求
post *****************
host: **************
content-type: text/xml; charset=utf-8
content-length: length
soapaction: "http://*******/downloadfilebymaterialcode"
<?xml version="1.0" encoding="utf-8"?>
<soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<downloadfilebymaterialcode xmlns="http://*******/">
<materialcode>string</materialcode>
<filetype>string</filetype>
<category>string</category>
</downloadfilebymaterialcode>
</soap:body>
</soap:envelope>
响应
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<downloadfilebymaterialcoderesponse xmlns="http://********/">
<downloadfilebymaterialcoderesult>string</downloadfilebymaterialcoderesult>
</downloadfilebymaterialcoderesponse>
</soap:body>
</soap:envelope>根据我的这个示例,我创建的请求类和响应类,是这样的
请求类
@data
@xmlrootelement(name = "downloadfilebymaterialcode", namespace = "http://*******/")
@xmlaccessortype(xmlaccesstype.field)
public class downloadfilebymaterialcoderequest {
@xmlelement(name = "materialcode", namespace = "http://*******/")
private string materialcode;
@xmlelement(name = "filetype", namespace = "http://*******/")
private string filetype;
@xmlelement(name = "category", namespace = "http://*******/")
private string category;
}响应类
@data
@xmlrootelement(name = "downloadfilebymaterialcoderesponse", namespace = "http://********/")
@xmlaccessortype(xmlaccesstype.field)
public class downloadfilebymaterialcoderesponse {
@xmlelement(name = "downloadfilebymaterialcoderesult", namespace = "http://********/")
private string downloadfilebymaterialcoderesult;
}三.创建objectfactory类
@xmlregistry
public class objectfactory {
// 创建 downloadfilebymaterialcoderequest 的实例
public downloadfilebymaterialcoderequest createdownloadfilebymaterialcoderequest() {
return new downloadfilebymaterialcoderequest();
}
// 创建 downloadfilebymaterialcoderesponse 的实例
public downloadfilebymaterialcoderesponse createdownloadfilebymaterialcoderesponse() {
return new downloadfilebymaterialcoderesponse();
}
}四.配置webservicetemplate
@configuration
public class webserviceconfig {
@bean
public webservicetemplate webservicetemplate() {
jaxb2marshaller marshaller = new jaxb2marshaller();
marshaller.setcontextpath("org.jeecg.modules.webservice"); // 包路径,包含请求和响应对象类
webservicetemplate template = new webservicetemplate(marshaller);
return template;
}
}五.调用webservice接口
@service
public class downloadfileservice {
@autowired
private webservicetemplate webservicetemplate;
public list<responsejsonobject> downloadfile(string materialcode, string filetype, string category) throws jsonprocessingexception {
string uri = "http://192.168.***.***/dydservicetest/plmservice.asmx"; // webservice 的 url
// 创建请求对象并设置参数
downloadfilebymaterialcoderequest request = new downloadfilebymaterialcoderequest();
request.setmaterialcode(materialcode);
request.setfiletype(filetype);
request.setcategory(category);
// 设置 soapaction
string soapaction = "http://********/downloadfilebymaterialcode"; // web 服务指定的 soapaction
// 使用 soapactioncallback 来设置 soapaction 头
soapactioncallback soapactioncallback = new soapactioncallback(soapaction);
// 发送 soap 请求并获取响应
downloadfilebymaterialcoderesponse response = (downloadfilebymaterialcoderesponse)
webservicetemplate.marshalsendandreceive(uri, request, soapactioncallback);
// 获取并返回 downloadfilebymaterialcoderesult
string downloadfilebymaterialcoderesult = response.getdownloadfilebymaterialcoderesult();
system.out.println(downloadfilebymaterialcoderesult);
//字符串转换为responsejsonobject对象
objectmapper objectmapper = new objectmapper();
// 忽略未知字段
objectmapper.configure(deserializationfeature.fail_on_unknown_properties, false);
list<responsejsonobject> responsejsonobjects = objectmapper.readvalue(downloadfilebymaterialcoderesult, objectmapper.gettypefactory().constructcollectiontype(list.class, responsejsonobject.class));
return responsejsonobjects;
}
}六.测试代码
@test
public void test1() throws jsonprocessingexception {
list<responsejsonobject> responsejsonobjects = downloadfileservice.downloadfile("ccpt0016-qby-7", "", "");
for (responsejsonobject responsejsonobject : responsejsonobjects) {
system.out.println(responsejsonobject.getdocname());
}
}测试效果

这里在附上所有文件的路劲图,可以参考一下

总结
根据接口给出的saop的示例,封装好对应的实体类,因为我这里的类型都是string,大家也可以根据实际情况,封装好对应的类
注意注解的参数,namespace = "http://*******/" 给接口提供的域名地址
到此这篇关于springboot调用webservice接口的文章就介绍到这了,更多相关springboot调用webservice接口内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论