AXIS调用jws发布的webservice
参考链接:
http://www.cnblogs.com/Gyoung/p/5489010.html
http://blog.csdn.net/hewei0241/article/details/24702731
http://blog.csdn.net/xdd19910505/article/details/50127999
使用到的jar包
maven
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
生成服务
WebserviceServer.java
注释 | 表示 |
---|---|
@WebParam | 使生成的参数名为name |
@WebMethod | 方法注释 |
@WebService | 服务注释 |
package test;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class WebserviceServer {
@WebMethod
public String Hello(@WebParam(name="name") String name){
return "AXIS say hello to " + name;
}
public static void main(String[] args) {
WebserviceServer server = new WebserviceServer();
Endpoint.publish("http://localhost:8080/test", server);
}
}
生成结果:
http://localhost:8080/test?wsdl
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test/" name="WebserviceServerService">
<types/>
<message name="Hello">
<part name="name" type="xsd:string"/>
</message>
<message name="HelloResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="WebserviceServer">
<operation name="Hello">
<input wsam:Action="http://test/WebserviceServer/HelloRequest" message="tns:Hello"/>
<output wsam:Action="http://test/WebserviceServer/HelloResponse" message="tns:HelloResponse"/>
</operation>
</portType>
<binding name="WebserviceServerPortBinding" type="tns:WebserviceServer">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="Hello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://test/"/>
</input>
<output>
<soap:body use="literal" namespace="http://test/"/>
</output>
</operation>
</binding>
<service name="WebserviceServerService">
<port name="WebserviceServerPort" binding="tns:WebserviceServerPortBinding">
<soap:address location="http://localhost:8080/test"/>
</port>
</service>
</definitions>
客户端读取
WebClient.java
package test;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;
/**
* Created by jaer on 2016/12/19.
*/
public class WebClient {
public static final WebClient instance = new WebClient();
public String invokeRemoteFuc() {
String endpoint="http://localhost:8080/test";
String result="no result!";
System.out.println(result);
Service service =new Service();
Call call;
Object[] object=new Object[1]; //Object用来存储方法的参数
object[0]="张三"; //相应的参数
try {
call=(Call)service.createCall();
call.setTargetEndpointAddress(endpoint); //远程调用路径
call.setOperationName(new QName("http://test/", "Hello"));//调用的方法名
//设置参数名:
call.addParameter("name", //参数名
XMLType.XSD_STRING,//参数类型:String
ParameterMode.IN); //参数模式:'IN' or 'OUT'
//设置返回值类型:
call.setReturnType(XMLType.XSD_STRING); //返回值类型:String
result=(String) call.invoke(object); //远程调用
}catch (ServiceException e) {
e.printStackTrace();
}catch (RemoteException e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
String result =instance.invokeRemoteFuc();
System.out.println(result);
}
}
输出结果
no result!
AXIS say hello to 张三