java_调用Webservice接口
Java以webservice方式调用SAP接口传输数据

Java以webservice方式调用SAP接口传输数据,在SAP中生成会计凭证/冲销会计凭证1.生成会计凭证(已完成)2.冲销会计凭证(以下教程截图以该接口为例)Java调用sap的webservie接口,即SAP作为webservice的服务端,java程序作为调用webservice服务的客户端。
1.获取SAP的webservice服务端接口的描述文件-WSDL文件;2.在apache的axis项目官方网站上下载axis代码工具包3.通过axis代码工具包和拿到的接口的WSDL文件,生成java的webservice客户端代码新建windows的批处理文件,内容如下set Axis_Lib=D:\axis-1_4\libset Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%set Output_Path=D:\axis-1_4\server%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% D:\axis-1_4\BC_FIDocCreate_Sender_SI_FIDocRev_OB.wsdl@pause将该批处理文件存储,然后在命令行窗口运行在D:\axis-1_4下生成了server目录,里面就是生成的客户端代码4.在MyEclipse中新建java web工程(??此处不是很确定,应该java工程也可以,在javaweb工程中也是直接通过main函数run的),将生成的webservice客户端代码导入,注意修改对应的HTTP和HTTPS地址,调用客户端请求数据参考附件打包的java工程代码Build Path设置,lib目录里面有很多jar包,实际该工程用到的没几个,如下图5.参看文档地址/bp-ab0a8b2dcfc789eb172dc814-1.html6.异常解决确保到目标地址的物理网络和对应端口没有被网管或防火墙屏蔽,如果请求webservice的服务器端有问题,可以根据java中抛出的异常信息跟踪和修改Webservice的调用本质上是一个http请求和响应的过程,可以在java代码中设置代理,然后使用http代理服务器软件记录http请求的请求头数据和返回的数据,跟踪异常情况,http代理服务器软件推荐burpsuit(黑客级工具,很好很强大,可以到网上搜破解版,如何设置和使用,请百度),通过代理服务器得到的http请求的请求头数据和返回的数据如下图附件java工程源代码下载地址链接: /s/1sjJnQMD密码: 6q3d作者联系方式:newker@ 百度空间: /newker1989。
resttemplate方式调用webservice

resttemplate方式调用webservice在Java中,RestTemplate是一个非常方便的HTTP客户端,可以用来调用RESTful风格的Web服务。
以下是如何使用RestTemplate调用Webservice的步骤:首先,需要导入相关的依赖包。
在Maven项目中,可以在pom.xml文件中添加以下依赖:```xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>```然后,你可以创建一个RestTemplate的Bean,并在需要的地方注入它。
如果你正在编写一个Spring Boot应用,你可以在Application类中创建一个RestTemplate的Bean:```javaimport org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class AppConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}}```然后,在你需要调用Web服务的地方,注入RestTemplate,并使用它来发送HTTP 请求。
例如,如果你想调用一个GET请求到Web服务,你可以这样做:```javaimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.client.RestTemplate;import org.springframework.web.util.UriComponentsBuilder;public class MyServiceCaller {private final RestTemplate restTemplate;@Autowiredpublic MyServiceCaller(RestTemplate restTemplate) {this.restTemplate = restTemplate;}public String callWebService(String url) {String result = restTemplate.getForObject(url, String.class); return result;}}```在上述代码中,我们使用了RestTemplate的getForObject方法来发送一个GET 请求到Web服务。
WEBSERVICE接口实例说明文档

WebService接口说明文档文档说明本文档主要讲述如何用C Sharp创建一个简单的Web Service接口,并使用Java调用这个Web Service接口。
准备工作系统环境:安装JDK1.6或更新版本开发工具:Microsoft Visual Studio2012、MyEclipse10.5、axis2-1.6.2C Sharp服务端1.首先,创建一个Web Service项目。
依次点击:文件—新建—项目,在弹出的新建项目窗口中选择Web下的空Web应用程序。
如下图:2.接下来我们需要创建我们的Web Service接口实现文件。
鼠标右击我们的项目,依次点击:添加—新建项,在弹出窗口中选择Web服务。
可修改新建项的文件名,注意文件名后缀后.asmx。
如下图:新建完成后我们的项目结构如下:3.打开我们新建的MyService.asmx下的MyService.asmx.cs文件,可以看到其中已经有默认的HelloWorld方法。
我们可以直接运行查看下运行的效果,效果如下图:点击HelloWorld,再点击调用可以看到页面返回:4.接下来我们完善我们的Web Service接口功能。
主要对Web Service接口进行参数类型的测试,文本型、布尔型、数值型、类(Class)等。
新增Add()等运算方法:新增strcat()连接字符串方法:新增GetBool()返回布尔值方法:新增GetTest()返回测试类,并新增Test类运行我们的项目,可以看到我们的结果如下图:点击add方法测试:输入add的参数i和j点击调用按钮,可以看到返回计算结果:5.到此为止我们C Sharp创建的Web Service程序完成。
接下来看Java如何调用我们的Web Service接口。
Java调用Web Service准备对于一个Web Service接口我们当前只知道访问的地址,我们如何用Java去访问接口呢?已知Web Service接口地址:http://localhost:11766/MyService.asmx?WSDLJava调用Web Service的方式有以下1.使用HttpClient2.使用Xfire3.使用axis2本文档示例选择的是axis2,首先到axis2网站下载axis2,网址:/axis2/java/core/download.cgi当前最新版本1.6.2。
wsimport 调用webservice示例

wsimport 调用webservice示例wsimport是Java 提供的一个工具,用于生成与Web Services Description Language (WSDL) 文件相对应的Java 代码,以便客户端可以通过该代码调用Web 服务。
下面是一个简单的示例,演示如何使用wsimport工具调用Web 服务。
1.创建Web 服务:首先,确保你有一个可用的Web 服务,并获取其WSDL 地址。
你可以在浏览器中直接访问WSDL 地址,以获取WSDL 文件的内容。
2.使用wsimport 生成客户端代码:打开终端或命令行窗口,使用以下命令生成客户端代码:wsimport -d . -p com.example.client http://your-web-service-url?wsdl其中,-d用于指定生成的类文件的输出目录,-p用于指定生成的Java 类的包名,而http://your-web-service-url?wsdl是你的Web 服务的WSDL 地址。
3.编写客户端代码:创建一个Java 类,用于实际调用Web服务。
以下是一个简单的示例:package com.example.client;public class WebServiceClient {public static void main(String[] args) {// 创建Web 服务客户端对象YourWebService service = new YourWebService();YourWebServicePortType port =service.getYourWebServicePort();// 调用Web 服务方法String result = port.yourWebServiceMethod("Hello, Web Service!");// 处理返回结果System.out.println("Web Service Response: " + result);}}4.编译和运行客户端代码:编译生成的客户端代码:javac com/example/client/*.java运行客户端代码:java com.example.client.WebServiceClient这会执行客户端代码,调用Web 服务的方法并输出结果。
java axis webservice 传参方法

java axis webservice 传参方法一、简介Java Axis Webservice 是一种常用的 Webservice 开发框架,用于创建和发布 Webservice。
在 Java Axis Webservice 中,参数传递是一种重要的技术,它决定了服务调用的方式和方法。
本文档将详细介绍 Java Axis Webservice 中参数传递的方法和技巧。
二、参数传递方式1. 简单方式:在 Webservice 客户端中,可以直接将参数作为请求体传递给 Webservice 接口方法。
这种方式适用于参数较少的情况。
2. 复杂方式:对于参数较多的情况,可以使用 Java Bean 或其他数据结构来封装参数,并将其作为请求体传递给 Webservice 接口方法。
3. 命名参数:在 Webservice 接口方法中,可以使用命名参数来接收参数。
这种方式适用于参数名称固定的场景。
4. 位置参数:在 Webservice 接口方法中,可以使用位置参数来接收参数。
这种方式适用于参数顺序固定的场景。
三、参数传递示例下面是一个简单的 Java Axis Webservice 客户端调用示例,演示了如何传递参数:1. 服务提供者(Service Provider)代码:```javapublic interface MyService extends javax.xml.rpc.Service {public void myMethod(String param1, int param2);}```2. 服务客户端(Service Client)代码:```javaMyService service = new MyService();MyService_PortType port = service.getMyService_Port();String value1 = "Hello";int value2 = 123;port.myMethod(value1, value2);```在这个示例中,我们使用了简单方式传递参数。
hutool webservice 调用方式

Hutool是一个Java工具库,它提供了许多实用的工具类和方法,可以简化Java开发中的常见任务。
关于Hutool中的Webservice调用方式,可以参考以下步骤:
1.引入Hutool依赖:在项目的构建文件(如Maven的pom.xml或Gradle的
build.gradle)中添加Hutool的依赖。
2.创建Webservice客户端:使用Hutool提供的工具类创建Webservice客户端对象。
Hutool提供了多种方式来创建Webservice客户端,例如使用Hutool提供的HttpClient或者通过使用Java内置的HttpURLConnection。
3.调用Webservice方法:使用创建的Webservice客户端对象调用Webservice方法。
调用方法时需要指定Webservice的URL、方法名以及传递的参数等。
4.处理返回结果:根据Webservice方法的返回类型,对返回的结果进行处理。
如果返回
的是XML数据,可以使用Hutool提供的XML解析工具类进行解析。
需要注意的是,具体的调用方式可能会根据Webservice的具体实现和要求有所不同。
因此,在实际使用中,建议仔细阅读Hutool的文档和示例代码,并根据实际情况进行调整和修改。
java使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例
java使用 CXF将 wsdl文件生成客户端代码命令 java调用第三方的 webservice应 用 实 例
cxf生成客户端代码 3、使用wsdl2java命令生成客户端代码 在命令行执行wsdl2java -p org.logink.ws -d f:\logink\src -all E:\wsdl3.1-credit\LogisticsCreditService\LogisticsCreditService.wsdl
wsdl2java用法: wsdl2java -p com -d src -all aa.wsdl -p 指定其wsdl的命名空间,也就是要生成代码的包名: -d 指定要产生代码所在目录 -client 生成客户端测试web service的代码 -server 生成服务器启动web service的代码 -impl 生成web service的实现代码 -ant 生成build.xml文件 -all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
webservice soap调用
webservice soap调用Webservice是一种基于web的远程通信技术,它通过标准的XML 消息进行通信,可以跨越平台和语言的限制,实现不同系统之间的互联互通。
SOAP是其中一种实现方式,它使用了XML格式的消息来描述操作和数据,通过HTTP或其他协议进行网络传输。
本文将介绍如何使用SOAP调用webservice服务。
SOAP调用webservice需要依照以下步骤进行:1. 找到webservice服务的WSDL文件,这个文件描述了服务的接口和操作。
2. 使用SOAP工具生成客户端代码,如Java中的wsimport命令。
这个步骤会自动生成可以调用服务的Java类文件。
3. 在Java代码中使用生成的类文件实例化服务端口,并为要调用的操作设置参数。
4. 调用服务端口的相应操作,获取返回结果。
下面我们来详细讲解一下这些步骤。
首先,在webservice服务提供方的网站中找到WSDL文件链接。
打开WSDL文件后,查看其中的接口和操作,了解服务提供方的数据类型和函数。
然后,在本地使用wsimport命令生成客户端代码。
在命令行终端中输入:wsimport -keep [WSDL文件链接]命令执行完毕后,会生成很多Java类文件,这些类可以直接被我们调用。
接着,我们在Java代码中调用生成的客户端类。
先实例化服务端口,再为要调用的操作设置参数。
例如,如果我们要调用一个返回天气的接口,代码如下:WeatherServicePortType weatherService = new WeatherService().getWeatherServiceHttpPort();String city = "Beijing";String weather = weatherService.getWeather(city);最后,调用服务端口的操作并获取返回结果。
在上面的代码中,我们调用了WeatherService服务的getWeather方法,并传入了一个城市参数“Beijing”,获取对应的天气信息。
WebService接口的几种调用方式--wsdl文件类型
WebService接⼝的⼏种调⽤⽅式--wsdl⽂件类型⾸先⽐如查询到的⼀个wsdl⽂件内容如下:<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions targetNamespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:apachesoap="/xml-soap" xmlns:impl="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:intf="http://198.168.0.88:68 <!--WSDL created by Apache Axis version: 1.4Built on Apr 22, 2006 (06:55:48 PDT)--><wsdl:types><schema targetNamespace="urn:client" xmlns="/2001/XMLSchema"><import namespace="/soap/encoding/"/><complexType name="WSContext"><sequence><element name="dbType" type="xsd:int"/><element name="dcName" nillable="true" type="xsd:string"/><element name="password" nillable="true" type="xsd:string"/><element name="sessionId" nillable="true" type="xsd:string"/><element name="slnName" nillable="true" type="xsd:string"/><element name="userName" nillable="true" type="xsd:string"/></sequence></complexType></schema></wsdl:types><wsdl:message name="logoutResponse"><wsdl:part name="logoutReturn" type="xsd:boolean"></wsdl:part></wsdl:message><wsdl:message name="loginResponse"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="loginRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part><wsdl:part name="authPattern" type="xsd:string"></wsdl:part></wsdl:message><wsdl:message name="loginResponse1"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="logoutRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part></wsdl:message><wsdl:message name="loginResponse2"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="loginRequest1"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginByLtpaTokenRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="ltpaToken" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginRequest2"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part><wsdl:part name="authPattern" type="xsd:string"></wsdl:part><wsdl:part name="isEncodePwd" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginByLtpaTokenResponse"><wsdl:part name="loginByLtpaTokenReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:portType name="EASLoginProxy"><wsdl:operation name="logout" parameterOrder="userName slnName dcName language"><wsdl:input message="impl:logoutRequest" name="logoutRequest"></wsdl:input><wsdl:output message="impl:logoutResponse" name="logoutResponse"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern"> <wsdl:input message="impl:loginRequest" name="loginRequest"></wsdl:input><wsdl:output message="impl:loginResponse" name="loginResponse"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType"><wsdl:input message="impl:loginRequest1" name="loginRequest1"></wsdl:input><wsdl:output message="impl:loginResponse1" name="loginResponse1"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern isEncodePwd"><wsdl:input message="impl:loginRequest2" name="loginRequest2"></wsdl:input><wsdl:output message="impl:loginResponse2" name="loginResponse2"></wsdl:output></wsdl:operation><wsdl:operation name="loginByLtpaToken" parameterOrder="userName ltpaToken slnName dcName language dbType"><wsdl:input message="impl:loginByLtpaTokenRequest" name="loginByLtpaTokenRequest"></wsdl:input><wsdl:output message="impl:loginByLtpaTokenResponse" name="loginByLtpaTokenResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="EASLoginSoapBinding" type="impl:EASLoginProxy"><wsdlsoap:binding style="rpc" transport="/soap/http"/><wsdl:operation name="logout"><wsdlsoap:operation soapAction=""/><wsdl:input name="logoutRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="logoutResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest1"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse1"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest2"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse2"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="loginByLtpaToken"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginByLtpaTokenRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginByLtpaTokenResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="EASLoginProxyService"><wsdl:port binding="impl:EASLoginSoapBinding" name="EASLogin"><wsdlsoap:address location="http://198.168.0.88:6888/ormrpc/services/EASLogin"/></wsdl:port></wsdl:service></wsdl:definitions>⼀般有⼏种⽅式调⽤此接⼝:⽅式⼀:通过wsdl⽂件或者链接使⽤相关IDE软件⽣成Java⽂件这种⽅式⽐较累赘,不做推荐,使⽤IDE软甲即可完成⽅式⼆:使⽤apache的动态代理⽅式实现话不多说,直接上代码:import .URL;import space.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class UsingDII {public static void main(String[] args) {try {int a = 100, b=60; // 对应的targetNamespaceString endPoint = "http://198.168.0.88:6888/ormrpc/services/EASLogin";Service service = new Service();Call call = (Call)service.createCall();call.setOperationName(new QName(endPoint,"EASLogin"));call.setTargetEndpointAddress(new URL(endPoint)); // a,b 调⽤此⽅法的参数String result = (String)call.invoke(new Object[]{new Integer(a),new Integer(b)});System.out.println("result is :"+result);} catch (Exception e) {e.printStackTrace();}}}⽅式三:使⽤Dynamic Proxy动态代理import .URL;import javax.xml.*;public class UsingDP {public static void main(String[] args) {try {int a = 100, b=60;String wsdlUrl = "http://198.168.0.88:6888/ormrpc/services/EASLogin?wsdl";String nameSpaceUri = "http://198.168.0.88:6888/ormrpc/services/EASLogin";String serviceName = "EASLoginProxyService";String portName = "EASLogin";ServiceFactory serviceFactory = ServiceFactory.newInstance();Service service = serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri,serviceName)); // 返回值是⾃⼰封装的类AddFunctionServiceIntf adsIntf = (AddFunctionServiceIntf)service.getPort(new QName(nameSpaceUri,portName),AddFunctionServiceIntf.class); System.out.println("result is :"+adsIntf.addInt(a, b));} catch (Exception e) {e.printStackTrace();}}}⽅式四:使⽤httpclient⽅式参考:直接上代码:import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import mons.httpclient.HttpClient;import mons.httpclient.methods.InputStreamRequestEntity;import mons.httpclient.methods.PostMethod;import mons.httpclient.methods.RequestEntity;import org.apache.log4j.Logger;import org.dom4j.Document;import org.dom4j.io.SAXReader;// 这⾥引得依赖包的话需要⾃⼰找了下⾯地址可以找到//https:///public static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{PostMethod postMethod = new PostMethod(requestUrl);byte[] b = xmlData.getBytes("utf-8");InputStream is = new ByteArrayInputStream(b, 0, b.length);RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8");postMethod.setRequestEntity(re);HttpClient httpClient = new HttpClient();httpClient.getParams().setAuthenticationPreemptive(true);httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT")));int statusCode = httpClient.executeMethod(postMethod);logger.debug("responseCode:"+statusCode);if (statusCode != 200) {return null;}return postMethod.getResponseBodyAsStream();}public static void main(String[] args) {String reqJsonStr = "{\"workId\":\"20171018161622\",\"status\":\"201\",\"startTime\":\"2017-10-18 16:16:22\"}";String xmlData = "<soapenv:Envelope xmlns:soapenv=\"/soap/envelope/\" xmlns:ser=\"/\"><soapenv:Header/><soapenv:Body><ser:statusWriteBack><jsonString>" + "{\"workId\":\"314\",\"orderId\":\"5207675\",\"longitude\":\"104.068310\",\"latitude\":\"30.539503\",\"sendTime\":\"2019-08-13 08:38:45\",\"servicePerName\":\"于xx\",\"servicePerPhone\":\"184xxxx7680\"}"+ "</jsonString></ser:statusWriteBack></soapenv:Body></soapenv:Envelope>";String url = "http://xx.xxx.246.88:7103/avs/services/CCService?wsdl";SAXReader reader = new SAXReader();String result = "";try {InputStream in = postXmlRequestInputStream(url,xmlData);if(in!=null){Document doc = reader.read(in);result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText();logger.debug("result:"+result);}} catch (Exception e) {logger.error("error:",e);e.printStackTrace();}}}CommonPptsUtil://就是获取配置⽂件⾥的代理信息import mons.configuration.ConfigurationException;import mons.configuration.PropertiesConfiguration;import mons.configuration.reloading.FileChangedReloadingStrategy;import org.apache.log4j.Logger;/*** 通⽤属性⽂件⼯具类** @author y.c**/public class CommonPptsUtil {private static final Logger logger = Logger.getLogger(CommonPptsUtil.class);private static final String CONFIG_FILE = "common.properties";private static PropertiesConfiguration ppts;static {try {ppts = new PropertiesConfiguration(CONFIG_FILE);ppts.setReloadingStrategy(new FileChangedReloadingStrategy());} catch (ConfigurationException e) {logger.error("⽂件【common.properties】加载失败!");ppts = null;}}/*** 获取属性值** @param key* @return属性值*/public static String get(String key) {if (ppts == null) {return null;}return ppts.getString(key);}}⽅式五:使⽤CXF动态调⽤webservice接⼝代码如下:参考:package cxfClient;import org.apache.cxf.endpoint.Endpoint;import space.QName;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import org.apache.cxf.service.model.BindingInfo;import org.apache.cxf.service.model.BindingOperationInfo;public class CxfClient {public static void main(String[] args) throws Exception {String url = "http://localhost:9091/Service/SayHello?wsdl";String method = "say";Object[] parameters = new Object[]{"我是参数"};System.out.println(invokeRemoteMethod(url, method, parameters)[0]);}public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();if (!url.endsWith("wsdl")) {url += "?wsdl";}org.apache.cxf.endpoint.Client client = dcf.createClient(url);//处理webService接⼝和实现类namespace不同的情况,CXF动态客户端在处理此问题时,会报No operation was found with the name的异常Endpoint endpoint = client.getEndpoint();QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation);BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding();if(bindingInfo.getOperation(opName) == null){for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){if(operation.equals(operationInfo.getName().getLocalPart())){opName = operationInfo.getName();break;}}}Object[] res = null;try {res = client.invoke(opName, parameters); } catch (Exception e) {e.printStackTrace();}return res;}}。
[Java]java调用wsdl接口
步骤2:将生成的java类文件导入我们的项目,查看其结构。
一般,服务都叫XXXService,这是我们最为关心的接口文件 步骤3:查看service的文档结构
红框的两个方法一个是实例化service的方法,一个是调用具体接口的方法
步骤4:编写工具类测试接口调用情况
网络错误421请刷新页面重试持续报错请尝试更换浏览器或网络环境
前提: ① 已经提供了一个wsdl接口 ② 该接口能正常调用
[Java]java调用 wsdl接口
步骤1:使用cxf的wsdl2java工具生成本bService\CXF\apache-cxf-2.1.1\apache-cxf-2.1.1 PATH后追加上“ ;%CXF_HOME%\bin” 验证:cmd命令行中输入wsdl2java,如果显示其用法表示配置好了。 运行命令 : wsdl2java .wsdl (wsdl 的路径) 运行后会在运行命令的当前路径生成以供使用的类
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java 调用webservice的各种方法总结 现在webservice加xml技术已经逐渐成熟,但要真正要用起来还需时日!! 由于毕业设计缘故,我看了很多关于webservice方面的知识,今天和大家一起来研究研究webservice的各种使用方法。
一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务 1.首先建立一个Web services EndPoint: package Hello; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint;
@WebService public class Hello {
@WebMethod public String hello(String name) { return "Hello, " + name + "\n"; }
public static void main(String[] args) { // create and publish an endpoint Hello hello = new Hello(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello); } } 2.使用 apt 编译 Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录 3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示
4.使用wsimport 生成客户端 使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl 这时,会在当前目录中生成如下文件: 5.客户端程序: 1class HelloClient{ 2public static void main(String args[]) { 3 HelloService service = new HelloService(); 4 Hello helloProxy = service.getHelloPort(); 5 String hello = helloProxy.hello("你好"); 6 System.out.println(hello); 7 } 8} 9 以上方法还稍显繁琐,还有更加简单的方法
二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的 利用xfire开发WebService,可以有三种方法: 1一种是从javabean 中生成; 2 一种是从wsdl文件中生成; 3 还有一种是自己建立webservice 步骤如下: 用myeclipse建立webservice工程,目录结构如下:
首先建立webservice接口, 代码如下:
1package com.myeclipse.wsExample; 2//Generated by MyEclipse 3 4public interface IHelloWorldService { 5 6 public String example(String message); 7 8} 接着实现这个借口: 1package com.myeclipse.wsExample; 2//Generated by MyEclipse 3 4public class HelloWorldServiceImpl implements IHelloWorldService { 5 6 public String example(String message) { 7 return message; 8 } 9 10} 修改service.xml 文件,加入以下代码: 1 2 HelloWorldService 3 4 com.myeclipse.wsExample.IHelloWorldService 5 6 7 com.myeclipse.wsExample.HelloWorldServiceImpl 8 9 10 literal 11 application 12 把整个项目部署到tomcat服务器中 ,打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:
然后再展开HelloWorldService后面的wsdl可以看到: 客户端实现如下: 1package com.myeclipse.wsExample.client; 2 3import java.net.MalformedURLException; 4import java.net.URL; 5 6import org.codehaus.xfire.XFireFactory; 7import org.codehaus.xfire.client.Client; 8import org.codehaus.xfire.client.XFireProxyFactory; 9import org.codehaus.xfire.service.Service; 10import org.codehaus.xfire.service.binding.ObjectServiceFactory; 11 12import com.myeclipse.wsExample.IHelloWorldService; 13 14public class HelloWorldClient { 15public static void main(String[] args) throws MalformedURLException, Exception { 16// TODO Auto-generated method stub 17Service s=new ObjectServiceFactory().create(IHelloWorldService.class); 18XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire()); 19String url="http://localhost:8989/HelloWorld/services/HelloWorldService"; 20 21 try 22 { 23 24 IHelloWorldService hs=(IHelloWorldService) xf.create(s,url); 25 String st=hs.example("zhangjin"); 26 System.out.print(st); 27 } 28 catch(Exception e) 29 { 30 e.printStackTrace(); 31 } 32 } 33 34} 35 这里再说点题外话,有时候我们知道一个wsdl地址,比如想用java客户端引用.net 做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下: 1public static void main(String[] args) throws MalformedURLException, Exception { 2 // TODO Auto-generated method stub 3 Service s=new ObjectServiceFactory().create(IHelloWorldService.class); 4 XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire()); 5 6 7//远程调用.net开发的webservice 8Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl")); 9 Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"}); 10 11//调用.net本机开发的webservice 12Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl")); 13Object[] o1=c1.invoke("HelloWorld",new String[]{}); 14 15} 三、使用axis1.4调用webservice方法 前提条件:下载axis1.4包和tomcat服务器 ,并将axis文件夹复制到tomcat服务器的webapp文件夹中 这里我就说一下最简单的方法: 首先建立一个任意的java类(例如:HelloWorld.java),复制到axis文件夹下,将其扩展名改为jws,然后重新启动tomcat,在浏览器中输入http://localhost:8989/axis/HelloWorld.jws?wsdl,就会得到一个wsdl文件,其客户端调用方法如下: 1import javax.xml.rpc.Service; 2import javax.xml.rpc.ServiceException; 3import javax.xml.rpc.ServiceFactory; 4 5import java.net.MalformedURLException; 6import java.net.URL; 7import java.rmi.RemoteException; 8 9import javax.xml.namespace.QName; 10 11public class TestHelloWorld { 12 13 14 public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException { 15 // TODO Auto-generated method stub 16 17 String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws?wsdl"; 18 String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws"; 19 String serviceName = "HelloWorldService"; 20 String portName = "HelloWorld"; 21