一.WebService發佈服務方式java
方式一:使用jre本身提供的方式 jax-wsapache
1)在類上或者相應類的接口或者抽象類上添加註解@WebServicejson
2)發佈服務 Endpoint.publish("http://127.0.0.1:8800/hello", new HelloServiceImpl());api
3) http://127.0.0.1:8800/hello?wsdl 能夠正常顯示說明發布服務成功frontend
方式二:使用CXF的方式發佈服務ide
1)須要引入相應的jar包url
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.2.3</version> </dependency>
2)在類上或者相應類的接口或者抽象類上添加註解@WebServicespa
3)發佈服務 Endpoint.publish("http://127.0.0.1:8800/hello", new HelloServiceImpl());.net
4) http://127.0.0.1:8800/hello?wsdl 能夠正常顯示說明發布服務成功 和 方式一 生成的wsdl文檔不太同樣了3d
二.WebService調用服務方式
方式一:使用jre本身提供的方式 jax-ws 能夠針對任何狀況
a.選擇新建 Web Service Client
b.輸入wsdl地址並點擊 Finish
c.調用生成的代碼
1.使用代理的方式
WSServerProxy wsServerProxy = new WSServerProxy(); String execBusinessMethod = wsServerProxy.execBusinessMethod("nice", "123213"); System.out.println(execBusinessMethod);
2.使用接口方式
//注意接口要和服務端一致的註解@WebService@WebParam.... URL url = new URL("http://127.0.0.1:8899/zl?wsdl"); //服務的wsdl文檔地址 QName qname=new QName("http://zl.com/","WSServerService"); //wsdl上的targetNamespace,和後面的name的值 Service service=Service.create(url, qname); WSServer ms=service.getPort(WSServer.class); String hello = ms.execBusinessMethod("good", "123123"); System.out.println(hello);
方式二:使用axis方式調用 只能是CXF的提供服務的方式
a.須要引用的jar
<!-- 引用的jar包 --> <!-- https://mvnrepository.com/artifact/axis/axis --> <dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.5</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1</version> </dependency>
b.調用服務代碼 注意包不要引錯
import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; //new 一個服務 Service sv = new Service(); //建立一個call對象 Call call = (Call) sv.createCall(); //設置要調用的接口地址 call.setTargetEndpointAddress(new URL("http://127.0.0.1:8899/hello")); //設置要調用的接口方法 call.setOperationName(new QName("getHello")); //設置參數 第二個參數表示String類型,第三個參數表示入參 call.addParameter("str", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); //返回參數類型 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //開始調用方法並返回相應數據信息,以xml格式的字符串返回,也能夠json格式主要看對方用什麼方式返回 Object result = call.invoke(new Object[]{"nice"}); System.out.println(result);//打印字符串