java调用WebService(未完成)记录篇
背景:
通过一个实例来认识webservice
服务端
public interface ExampleService {
String sayHi(String request);
}
@WebService
public class ExampleServiceImpl implements ExampleService{
@Override
public String sayHi(String request) {
return "request:"+request+"response:hi";
}
}
public class ExampleServe {
public static void main(String[] args) {
//接口发布的地址
Endpoint.publish("http://localhost:8080/example",new ExampleServiceImpl());
//查看WebService服务是否启动 url+?wsdl
//http://localhost:8080/example?wsdl是否能显示
System.err.println("服务发布成功");
}
}
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://summary.webservice.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="ExampleServiceImplService" targetNamespace="http://summary.webservice.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://summary.webservice.com/" elementFormDefault="unqualified" targetNamespace="http://summary.webservice.com/" version="1.0">
<xs:element name="sayHi" type="tns:sayHi"/>
<xs:element name="sayHiResponse" type="tns:sayHiResponse"/>
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHiResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHiResponse">
<wsdl:part element="tns:sayHiResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="sayHi">
<wsdl:part element="tns:sayHi" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="ExampleServiceImpl">
<wsdl:operation name="sayHi">
<wsdl:input message="tns:sayHi" name="sayHi"> </wsdl:input>
<wsdl:output message="tns:sayHiResponse" name="sayHiResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ExampleServiceImplServiceSoapBinding" type="tns:ExampleServiceImpl">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ExampleServiceImplService">
<wsdl:port binding="tns:ExampleServiceImplServiceSoapBinding" name="ExampleServiceImplPort">
<soap:address location="http://localhost:8080/example"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端
wsimport -s ./src/main/java/ -p com.webservice.summary.client -encoding utf-8 http://localhost:8080/example\?wsdl -d ./target/classes
会生成我们需要的文件,大致包含以下这些

public class ExampleClient {
public static void main(String[] args) {
ExampleServiceImplService exampleServiceImplService=new ExampleServiceImplService();
ExampleServiceImpl exampleServiceImplPort = exampleServiceImplService.getExampleServiceImplPort();
String result = exampleServiceImplPort.sayHi("这是参数");
System.out.println(result);
}
}
WebService的一些概念
踩坑之旅
务必保证提供方的wsdl可以通过浏览器打开
sap提供的wsdl
无法读取 WSDL 文档:*******
原因为 1) 找不到文档; 2) 无法读取文档; 3) 文档的根元素不是 <wsdl:definitions>。
[ERROR] failed.noservice=在提供的 WSDL 中找不到 wsdl:service:
需要至少提供一个 WSDL, 该 WSDL 至少具有一个服务定义。
-<wsdl

wsdl鉴权问题
wsimport使用authfile
wsimport -Xauthfile sap.txt http://*******.com:8001/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/yws_get_str/200/yws_get_str/yws_get_str?sap-client=200
test:123456@http://*******.com:8001/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/yws_get_str/200/yws_get_str/yws_get_str?sap-client=200
****************不是有效的授权信息格式。格式应为 http[s]://user:pa
ssword@host:port//<url-path>。
*************
需要授权, 请在C:\Us
ers\****\.metro\auth中提供具有读取访问权限的授权文件, 或者使用 -Xauthfile 指定授权文
件并在每一行上使用以下格式提供授权信息: http[s]://user:password@host:port//<url-path>
wsimport example.wsdl
方法重名
[WARNING] 忽略 SOAP 端口 "example_interface_12": 它使用非标准 SOAP 1.2 绑定。
必须指定 "-extension" 选项以使用此绑定。
正在生成代码...
[ERROR] 无法生成 SEI, 类com.sap.document.sap.soap.functions.mc_style.example_interface已存在
。请使用 JAX-WS 定制设置重命名 wsdl:portType "{urn:sap-com:document:sap:soap:functions:mc-style}ZC
example_interface"
为什么重?

解决方案


参数问题
鉴权:
public final class ExampleServiceImpl_ExampleServiceImplPort_Client {
private static final QName SERVICE_NAME = new QName("http://summary.webservice.com/", "ExampleServiceImplService");
private ExampleServiceImpl_ExampleServiceImplPort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = ExampleServiceImplService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
ExampleServiceImplService ss = new ExampleServiceImplService(wsdlURL, SERVICE_NAME);
ExampleServiceImpl port = ss.getExampleServiceImplPort();
{
System.out.println("Invoking sayHi...");
java.lang.String _sayHi_arg0 = "";
java.lang.String _sayHi__return = port.sayHi(_sayHi_arg0);
System.out.println("sayHi.result=" + _sayHi__return);
}
System.exit(0);
}
}
Server returned HTTP response code: 401 for URL
url = new URL("*******");
Authenticator.setDefault( new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username",
"passwd".toCharArray());
}
});
WSDL_LOCATION = url;
None of the policy alternatives can be satisfied.
final String username = "";
final String password = "";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(address);
factory.setServiceClass(ExampleServiceImpl.class);
factory.setUsername(username);
factory.setPassword(password);
ExampleServiceImpl bean = (ExampleServiceImpl) factory.create();
//组装参数
...
//组装参数结束
bean.hi(参数)
参数:
Web service processing error; more details in the web service error log on provider side
注意点
- 如果本来将代码放在A包下,因某些业务将代码迁移到B包下之后,记得修改interface类的@RequestWrapper和@ResponseWrapper后的className里的包名
- 在对应的serviceImpl里面:url = new URL("x"); x是wsdl的地址,如果是sap的话,那就是浏览器可以打开的那个地址
- factory.setAddress(address); 这里的address不是wsdl地址,而是你的wsdl和下图位置一样的地方的值

附录
Jax-ws
网络上可用的wsdl网站
wsimport 命令
wsimport -help 用法: wsimport [options] <WSDL_URI> \其中 [options] 包括:
-b <path> 指定 jaxws/jaxb 绑定文件或附加模式
(每个 <path> 都必须具有自己的 -b)
-B<jaxbOption> 将此选项传递给 JAXB 模式编译器
-catalog <file> 指定用于解析外部实体引用的目录文件
支持 TR9401, XCatalog 和 OASIS XML 目录格式。
-d <directory> 指定放置生成的输出文件的位置
-encoding <encoding> 指定源文件所使用的字符编码
-extension 允许供应商扩展 - 不按规范
指定功能。使用扩展可能会
导致应用程序不可移植或
无法与其他实现进行互操作
-help 显示帮助
-httpproxy:<host>:<port> 指定 HTTP 代理服务器 (端口默认为 8080)
-keep 保留生成的文件
-p <pkg> 指定目标程序包
-quiet 隐藏 wsimport 输出
-s <directory> 指定放置生成的源文件的位置
-target <version> 按给定的 JAXWS 规范版本生成代码
默认为 2.2, 接受的值为 2.0, 2.1 和 2.2
例如, 2.0 将为 JAXWS 2.0 规范生成兼容的代码
-verbose 有关编译器在执行什么操作的输出消息
-version 输出版本信息
-wsdllocation <location> @WebServiceClient.wsdlLocation 值
-clientjar <jarfile> 创建生成的 Artifact 的 jar 文件以及
调用 Web 服务所需的 WSDL 元数据。
-generateJWS 生成存根 JWS 实现文件
-implDestDir <directory> 指定生成 JWS 实现文件的位置
-implServiceName <name> 生成的 JWS 实现的服务名的本地部分
-implPortName <name> 生成的 JWS 实现的端口名的本地部分 \扩展:
-XadditionalHeaders 映射标头不绑定到请求或响应消息不绑定到
Java 方法参数
-Xauthfile 用于传送以下格式的授权信息的文件:
http://username:password@example.org/stock?wsdl
-Xdebug 输出调试信息
-Xno-addressing-databinding 允许 W3C EndpointReferenceType 到 Java 的绑定
-Xnocompile 不编译生成的 Java 文件
-XdisableAuthenticator 禁用由 JAX-WS RI 使用的验证程序,
将忽略 -Xauthfile 选项 (如果设置)
-XdisableSSLHostnameVerification 在提取 wsdl 时禁用 SSL 主机名
验证 \示例:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl
相关文档:
java调用WebService(未完成)记录篇的更多相关文章
- Axis2 webservice 之使用java调用webservice
在上一篇中写了一个简单了webservice,实现了一个sayHello功能.那么webservice写好之后我们如何使用Java程序来调用webservice呢? 一.java调用的webservi ...
- Java调用WebService方法总结(8)--soap.jar调用WebService
Apache的soap.jar是一种历史很久远的WebService技术,大概是2001年左右的技术,所需soap.jar可以在http://archive.apache.org/dist/ws/so ...
- Java调用WebService方法总结(6)--XFire调用WebService
XFire是codeHaus组织提供的一个WebService开源框架,目前已被Apache的CXF所取代,已很少有人用了,这里简单记录下其调用WebService使用方法.官网现已不提供下载,可以到 ...
- Java调用WebService方法总结(1)--准备工作
WebService是一种跨编程语言.跨操作系统平台的远程调用技术,已存在很多年了,很多接口也都是通过WebService方式来发布的:本系列文章主要介绍Java调用WebService的各种方法,使 ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- java 调用webservice的各种方法总结
java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...
- java调用webservice,restful
java调用webservice public String redoEsb(String loguid, String user, String comments, String newMsg, S ...
- 原生java调用webservice的方法,不用生成客户端代码
原生java调用webservice的方法,不用生成客户端代码 2015年10月29日 16:46:59 阅读数:1455 <span style="font-family: Aria ...
- Java调用Webservice(asmx)的几个例子
Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...
- Java调用WebService方法总结(9,end)--Http方式调用WebService
Http方式调用WebService,直接发送soap消息到服务端,然后自己解析服务端返回的结果,这种方式比较简单粗暴,也很好用:soap消息可以通过SoapUI来生成,也很方便.文中所使用到的软件版 ...
随机推荐
- solidity中的mapping
mapping可以理解为python中对字典的键值遍历,键是唯一的而值是可以重复的 mapping函数的构造: mapping(_KeyType => _ValueType) mapping ...
- 大语言模型快速推理: 在 Habana Gaudi2 上推理 BLOOMZ
本文将展示如何在 Habana Gaudi2 上使用 Optimum Habana.Optimum Habana 是 Gaudi2 和 Transformers 库之间的桥梁.本文设计并实现了一个大模 ...
- OpenWRT实现NAT64/DNS64
OpenWRT实现NAT64/DNS64 连接到核心路由器 # 连接到核心路由器 [C:\~]$ ssh root@10.0.0.1 Connecting to 10.0.0.1:22... Conn ...
- python文字转语音库及使用方法
作者:陈哲链接:https://www.zhihu.com/question/473797102/answer/2019063801来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- [JavaScript]Promise:异步编程
1 文由 某项目的需求:先要请求API1,再以API1的结果请求API2. var n, a; //var r = window.md5; var r = function (password, us ...
- vue-element-admin 动态菜单改造
vue-element-admin 动态菜单改造 vue-element-admin 是一款优秀后台前端解决方案,它基于 vue 和 element-ui实现.开源后台管理系统解决方案项目 Boot- ...
- Redis篇一之基础数据结构
文章目录 Redis的数据结构 String类型**** Hash类型 List类型 Set类型 SortedSet类型 BitMap类型 HyperLogLog 总结 Redis诞生于2009年全称 ...
- iframe分栏拖拽伸缩例子
这个标题有些绕口,鄙人愚笨,实在找不到一个比较准确的说法,总之就是: 一个页面内显示多个iframe,一个变宽,另一个就变窄,一个变高,另一个就变矮的这种可自由伸缩的效果.它们之间有一个可多拽的分隔条 ...
- SQLlabs less1-10通关笔记
SQLlabs 通关笔记 mysql数据结构 在练习靶场前我们需要了解以下mysql数据库结构,mysql数据库5.0以上版本有一个自带的数据库叫做information_schema,该数据库下面有 ...
- 2022-04-14:小美有一个长度为n的数组, 为了使得这个数组的和尽量大,她向会魔法的小团进行求助。 小团可以选择数组中至多两个不相交的子数组, 并将区间里的数全都变为原来的10倍。 小团想知道他
2022-04-14:小美有一个长度为n的数组, 为了使得这个数组的和尽量大,她向会魔法的小团进行求助. 小团可以选择数组中至多两个不相交的子数组, 并将区间里的数全都变为原来的10倍. 小团想知道他 ...