jax-rs jax-ws_Tomcat上具有JAX-WS的Web服务
jax-rs jax-ws
讓我們假設一家企業(yè)正在一個集中式系統(tǒng)中維護用戶身份驗證詳細信息。 我們需要創(chuàng)建一個AuthenticationService,它將獲取憑據(jù),對其進行驗證并返回狀態(tài)。 其余的應用程序將使用AuthenticationService對用戶進行身份驗證。
創(chuàng)建AuthenticationService接口,如下所示:
現(xiàn)在讓我們實現(xiàn)AuthenticationService。
package com.sivalabs.caas.services;import java.util.HashMap; import java.util.Map;import javax.jws.WebService;import com.sivalabs.caas.domain.AuthenticationStatus; import com.sivalabs.caas.domain.Credentials; import com.sivalabs.caas.exceptions.AuthenticationServiceException;/*** @author siva**/ @WebService(endpointInterface="com.sivalabs.caas.services.AuthenticationService",serviceName="AuthenticationService", targetNamespace="http://sivalabs.blogspot.com/services/AuthenticationService") public class AuthenticationServiceImpl implements AuthenticationService {private static final Map<string, string> CREDENTIALS = new HashMap<string, string>();static{CREDENTIALS.put("admin", "admin");CREDENTIALS.put("test", "test"); }@Overridepublic AuthenticationStatus authenticate(Credentials credentials) throws AuthenticationServiceException{if(credentials == null){throw new AuthenticationServiceException("Credentials is null");}AuthenticationStatus authenticationStatus = new AuthenticationStatus();String userName = credentials.getUserName();String password = credentials.getPassword();if(userName==null || userName.trim().length()==0 || password==null || password.trim().length()==0){authenticationStatus.setStatusMessage("UserName and Password should not be blank");authenticationStatus.setSuccess(false);}else{if(CREDENTIALS.containsKey(userName) && password.equals(CREDENTIALS.get(userName))){authenticationStatus.setStatusMessage("Valid UserName and Password");authenticationStatus.setSuccess(true);}else{authenticationStatus.setStatusMessage("Invalid UserName and Password");authenticationStatus.setSuccess(false);}}return authenticationStatus;} }為了簡單起見,我們在此根據(jù)存儲在HashMap中的靜態(tài)數(shù)據(jù)檢查憑據(jù)。 在實際的應用程序中,將根據(jù)數(shù)據(jù)庫進行此檢查。
現(xiàn)在,我們將發(fā)布WebService。
package com.sivalabs.caas.publisher;import javax.xml.ws.Endpoint;import com.sivalabs.caas.services.AuthenticationServiceImpl;public class EndpointPublisher {public static void main(String[] args){Endpoint.publish("http://localhost:8080/CAAS/services/AuthenticationService", new AuthenticationServiceImpl());}}運行此獨立的類以發(fā)布AuthenticationService。
要檢查服務是否已成功發(fā)布,請將瀏覽器指向URL http:// localhost:8080 / CAAS / services / AuthenticationService?wsdl。 如果該服務成功發(fā)布,您將看到WSDL內容。
現(xiàn)在,讓我們創(chuàng)建一個獨立測試客戶端來測試Web服務。
package com.sivalabs.caas.client;import java.net.URL;import javax.xml.namespace.QName; import javax.xml.ws.Service;import com.sivalabs.caas.domain.AuthenticationStatus; import com.sivalabs.caas.domain.Credentials; import com.sivalabs.caas.services.AuthenticationService;/*** @author siva**/ public class StandaloneClient {public static void main(String[] args) throws Exception{URL wsdlUrl = new URL("http://localhost:8080/CAAS/services/AuthenticationService?wsdl");QName qName = new QName("http://sivalabs.blogspot.com/services/AuthenticationService", "AuthenticationService");Service service = Service.create(wsdlUrl,qName);AuthenticationService port = service.getPort(AuthenticationService.class);Credentials credentials=new Credentials();credentials.setUserName("admin1");credentials.setPassword("admin");AuthenticationStatus authenticationStatus = port.authenticate(credentials);System.out.println(authenticationStatus.getStatusMessage());credentials.setUserName("admin");credentials.setPassword("admin");authenticationStatus = port.authenticate(credentials);System.out.println(authenticationStatus.getStatusMessage());}}不用我們自己編寫StandaloneClient,我們可以使用wsimport命令行工具生成Client。
wsimport工具在JDK / bin目錄中。
轉到項目的src目錄,然后執(zhí)行以下命令。
wsimport -keep -p com.sivalabs.caas.client http:// localhost:8080 / CAAS / services / AuthenticationService?wsdl
它將在com.sivalabs.caas.client軟件包中生成以下Java和類文件。
Authenticate.java
AuthenticateResponse.java
AuthenticationService_Service.java AuthenticationService.java AuthenticationServiceException_Exception.java AuthenticationServiceException.java AuthenticationStatus.java Credentials.java ObjectFactory.java 包信息.java
現(xiàn)在,您可以使用生成的Java文件來測試服務。
public static void main(String[] args) throws Exception {AuthenticationService_Service service = new AuthenticationService_Service();com.sivalabs.caas.client.AuthenticationService authenticationServiceImplPort = service.getAuthenticationServiceImplPort();com.sivalabs.caas.client.Credentials credentials = new com.sivalabs.caas.client.Credentials();credentials.setUserName("admin1");credentials.setPassword("admin");com.sivalabs.caas.client.AuthenticationStatus authenticationStatus = authenticationServiceImplPort.authenticate(credentials);System.out.println(authenticationStatus.getStatusMessage());credentials.setUserName("admin");credentials.setPassword("admin");authenticationStatus = authenticationServiceImplPort.authenticate(credentials);System.out.println(authenticationStatus.getStatusMessage()); }現(xiàn)在,我們將看到如何在Tomcat服務器上部署JAX-WS WebService。
我們將在apache-tomcat-6.0.32上部署在http://sivalabs.blogspot.com/2011/09/developing-webservices-using-jax-ws.html中開發(fā)的AuthenticationService。
要部署我們的AuthenticationService,我們需要添加以下配置。
1.web.xml
<web-app><listener><listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class></listener><servlet><servlet-name>authenticationService</servlet-name><servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>authenticationService</servlet-name><url-pattern>/services/AuthenticationService</url-pattern></servlet-mapping> </web-app>2.創(chuàng)建一個新文件WEB-INF / sun-jax-ws.xml
<?xml?version="1.0"?encoding="UTF-8"?> <endpointsxmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"version="2.0"><endpointname="AuthenticationService"implementation="com.sivalabs.caas.services.AuthenticationServiceImpl"url-pattern="/services/AuthenticationService"/></endpoints>3.從http://jax-ws.java.net/下載JAX-WS參考實現(xiàn)。
將所有jar文件從jaxws-ri / lib文件夾復制到WEB-INF / lib。
現(xiàn)在,將應用程序部署在Tomcat服務器上。
您不需要像使用EndpointPublisher那樣由我們自己發(fā)布服務。
Tomcat啟動并運行后,請在http:// localhost:8080 / CAAS / services / AuthenticationService?wsdl中查看生成的wsdl。
現(xiàn)在,如果您使用獨立客戶端測試AuthenticationService,它將可以正常工作。
public static void testAuthenticationService()throws Exception {URL wsdlUrl = new URL("http://localhost:8080/CAAS/services/AuthenticationService?wsdl");QName qName = new QName("http://sivalabs.blogspot.com/services/AuthenticationService", "AuthenticationService");Service service = Service.create(wsdlUrl,qName);AuthenticationService port = service.getPort(AuthenticationService.class);Credentials credentials=new Credentials();credentials.setUserName("admin");credentials.setPassword("admin");AuthenticationStatus authenticationStatus = port.authenticate(credentials);System.out.println(authenticationStatus.getStatusMessage()); }但是,如果嘗試使用wsimport工具生成的客戶端代碼進行測試,請確保在客戶端類路徑中沒有jax-ws-ri jar。
否則,您將得到以下錯誤:
Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String;at com.sun.xml.ws.model.RuntimeModeler.processExceptions(RuntimeModeler.java:1162)at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:898)參考: “ 我的實驗”博客上的 JCG合作伙伴 Siva Reddy提供了使用JAX-WS開發(fā)WebServices和在Tomcat-6上部署JAX-WS WebService的信息 。
翻譯自: https://www.javacodegeeks.com/2012/03/web-services-with-jax-ws-on-tomcat.html
jax-rs jax-ws
總結
以上是生活随笔為你收集整理的jax-rs jax-ws_Tomcat上具有JAX-WS的Web服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原型设计模式示例
- 下一篇: Android 系统被指存储空间计算存逻