1. 程式人生 > >web service 用JDK開發 版本在1.6以上

web service 用JDK開發 版本在1.6以上

color src ride poi gif port 實現類 調用服務 .org

-eclipse開發工具

裏面有自帶Web Services Explorer瀏覽器

在javaEE 下技術分享點擊右上角wsdlpage 即可

服務端代碼

技術分享

技術分享
package com.bdqn.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;


@WebService
public interface IholloTest {
   
    @WebMethod 
    String say(String str);
}
服務端接口 技術分享
package com.bdqn.ws;

import javax.jws.WebService;


@WebService
public class IHolleTest implements IholloTest{ @Override public String say(String str) { System.out.println("http://localhost:8989/FirstWS/adress"+str); return str; } }
服務端實現類 技術分享
package com.bdqn.ws;

import javax.xml.ws.Endpoint;

public class Publishs {

    public static
void main(String[] args) { String url="http://localhost:8989/FirstWS/adress" ; Endpoint.publish(url,new IHolleTest()); } }
endpoint 發布 技術分享
<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://ws.bdqn.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.bdqn.com/" name="IHolleTestService"> <types> <xsd:schema> <xsd:import namespace="http://ws.bdqn.com/" schemaLocation="http://localhost:8989/FirstWS/adress?xsd=1"/> </xsd:schema> </types> <message name="say"> <part name="parameters" element="tns:say"/> </message> <message name="sayResponse"> <part name="parameters" element="tns:sayResponse"/> </message> <portType name="IHolleTest"> <operation name="say"> <input wsam:Action="http://ws.bdqn.com/IHolleTest/sayRequest" message="tns:say"/> <output wsam:Action="http://ws.bdqn.com/IHolleTest/sayResponse" message="tns:sayResponse"/> </operation> </portType> <binding name="IHolleTestPortBinding" type="tns:IHolleTest"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="say"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="IHolleTestService"> <port name="IHolleTestPort" binding="tns:IHolleTestPortBinding"> <soap:address location="http://localhost:8989/FirstWS/adress"/> </port> </service> </definitions>
WSDL 文檔

在cmd 窗口用jdk 指令 生成客戶端的代碼

註意(這裏發布的url 是自己本機的IP)

這裏的wsdl文件 可以是 網絡的http://localhost:8989/FirstWS/adress?wsdl 也可以是項目中本地wsdl文件(是在網絡文件copy)

技術分享

執行完指令後生成com.bdqn.ws包下所有的類,--這裏的包名是來源於服務端的包名

技術分享

技術分享
package com.bdqn.client;

import com.bdqn.ws.IHolleTest;
import com.bdqn.ws.IHolleTestService;

public class Clientws {
 public static void main(String[] args) {
     IHolleTestService holle=new IHolleTestService();
      IHolleTest test=holle.getIHolleTestPort();
      System.out.println(test.getClass());
      String say=test.say("xiaoli ");
      System.out.println(say);
}
}
client 客戶端

啟動服務後客戶端方可調用服務端的代碼。

web service 用JDK開發 版本在1.6以上