JDK1.6生成webservice及訪問
webService是一種跨語言的系統間互動標準。在java中使用webservice根據伺服器端的服務根據描述生成WSDL檔案,並將應用與此WSDL檔案一起放入HTTP伺服器中,藉助服務工具根據WSDL檔案生成客戶端STUB程式碼。此程式碼的作用是將產生的物件請求資訊封裝成標準的SOAP格式資料,併發送到伺服器端,伺服器端根據接收到的SOAP格式資料進行轉換,並最終通過反射呼叫響應類的響應方法。
jdk 6中集成了WEB service的支援,通過WebService 的annotation來暴露服務的實現,並通過Endpoint.publish將服務釋出到指定的地址,客戶端通過wsimport來訪問響應地址的wsdl檔案,生成呼叫伺服器端服務的stub類資訊,客戶端即可通過生成的類來呼叫伺服器的服務了。
具體示例如下:
1.對外暴露的介面
package helloworld;
public interface TestWebService {
public String echo();
}
2.伺服器端的實現類,並通過@WebService來指定對外提供服務的服務名稱,客戶端生成的類目和包名
package helloworld; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(name="MyTestWS",serviceName="MyWebService",targetNamespace="http://localhost/client") public class WebServiceImpl implements TestWebService{ public String echo() { return "webservice return msg"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyWebService", new WebServiceImpl()); } }
3.然後執行伺服器的WebServiceImpl的main函式,暴露服務並將服務註冊到一個http服務地址上,客戶端通過jdk的bin下面的wsimport命令來獲取伺服器的wsdl檔案並生成客戶端的stub類資訊開啟dos視窗找到專案路徑執行一下程式碼
wsimport -keep http://localhost:8080/MyWebService?wsdl
此處可改為 wsimport -keep -p com.localhost.client http://localhost:8080/MyWebService?wsdl
com.localhost.client是自己定義的包預設用targetNamespace=
將出現編譯好的java檔案貼上到專案中
4.然後在你的路徑上就會生成下面幾個類
注:test.java 是測試類
5.然後我們編寫客戶端的呼叫程式碼
package localhost.client;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyWebService myservice = new MyWebService();
System.out.println(myservice.getMyTestWSPort().echo());
}
}
6.執行客戶端的呼叫程式碼,輸出如下:
配置web.xml
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>TestService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestService</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
在lib下建立sun-jaxws.xml
程式碼
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint name="TestService" implementation="helloworld.WebServiceImpl" url-pattern="/hello" />
</endpoints>
將專案放到tomcat下訪問
出現頁面
將MyWebService程式碼改為
package localhost.client;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebServiceClient(name = "MyWebService", targetNamespace = "http://localhost/client", wsdlLocation = "http://localhost/MyWebService?wsdl")
public class MyWebService
extends Service
{
private final static URL MYWEBSERVICE_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(localhost.client.MyWebService.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = localhost.client.MyWebService.class.getResource(".");
url = new URL(baseUrl, "http://localhost/javawebservice/hello?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost/MyWebService?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
MYWEBSERVICE_WSDL_LOCATION = url;
}
public MyWebService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public MyWebService() {
super(MYWEBSERVICE_WSDL_LOCATION, new QName("http://localhost/client", "MyWebService"));
}
/**
*
* @return
* returns MyTestWS
*/
@WebEndpoint(name = "MyTestWSPort")
public MyTestWS getMyTestWSPort() {
return super.getPort(new QName("http://localhost/client", "MyTestWSPort"), MyTestWS.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns MyTestWS
*/
@WebEndpoint(name = "MyTestWSPort")
public MyTestWS getMyTestWSPort(WebServiceFeature... features) {
return super.getPort(new QName("http://localhost/client", "MyTestWSPort"), MyTestWS.class, features);
}
}
再次呼叫test方法返回