天天看點

JAX-WS開發WebService示例

我們使用JAX-WS開發WebService隻需要很簡單的幾個步驟:寫接口和實作=>釋出=>生成用戶端(測試或使用)。

而在開發階段我們也不需要導入外部jar包,因為這些api都是現成的。首先是接口的編寫(接口中隻需要把類注明為@WebService,把 要暴露給用戶端的方法注明為@WebMethod即可,其餘如@WebResult、@WebParam等都不是必要的,而用戶端和服務端的通信用RPC 和Message-Oriented兩種,其他請查資料)

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
//@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorld {
    String sayHi(@WebParam(name = "who") String who);

    String sayHello(@WebParam(name = "who") String who);
}
           
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
//@WebService(endpointInterface = "com.conquer.comutils.core.jws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @WebMethod// 預設的,可省略
    @Override
    public String sayHi(String who) {
        return "hi, " + who;
    }

    // 排除的方法
    @WebMethod(exclude = true)
    @Override
    public String sayHello(@WebParam(name = "who") String who) {
        return "hello, " + who;
    }
}
           
import javax.xml.ws.Endpoint;

public class Main {
    static final String ADDRESS = "http://localhost:8080/test/jaxws/services/HelloWorld";

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorldImpl();
        Endpoint.publish(ADDRESS, helloWorld);
        System.out.println("JAX-WS WebService 服務已啟動");
    }
}
           

轉載于:https://my.oschina.net/u/1176770/blog/848428