創建AuthenticationService接口,如下所示:
package com.sivalabs.caas.services;
import javax.jws.WebService;import com.sivalabs.caas.domain.AuthenticationStatus;
import com.sivalabs.caas.domain.Credentials;
import com.sivalabs.caas.exceptions.AuthenticationServiceException;@WebService
public interface AuthenticationService
{
public AuthenticationStatus authenticate(Credentials credentials) throws AuthenticationServiceException;
}
package com.sivalabs.caas.domain;
/*** @author siva**/
public class Credentials
{private String userName;private String password;public Credentials()
{}public Credentials(String userName, String password) {super();this.userName = userName;this.password = password;}//setters and getters}
package com.sivalabs.caas.domain;/*** @author siva**/
public class AuthenticationStatus
{private String statusMessage;private boolean success;//setters and getters}
package com.sivalabs.caas.exceptions;/*** @author siva**/
public class AuthenticationServiceException extends RuntimeException
{private static final long serialVersionUID = 1L;public AuthenticationServiceException(){}public AuthenticationServiceException(String msg){super(msg);}
}
現在讓我們實現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;}
}
為了簡單起見,我們在此針對存儲在HashMap中的靜態數據檢查憑據。 在實際的應用程序中,將針對數據庫執行此檢查。
現在,我們將發布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());}}
運行此獨立的類以發布AuthenticationService。
要檢查服務是否已成功發布,請將瀏覽器指向URL http:// localhost:8080 / CAAS / services / AuthenticationService?wsdl。 如果服務成功發布,您將看到WSDL內容。
現在,讓我們創建一個獨立測試客戶端來測試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目錄并執行以下命令。
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
現在,您可以使用生成的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());
}
現在,我們將看到如何在Tomcat服務器上部署JAX-WS WebService。
我們將在apache-tomcat-6.0.32上部署在http://sivalabs.blogspot.com/2011/09/developing-webservices-using-jax-ws.html中開發的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.創建一個新文件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參考實現。
將所有jar文件從jaxws-ri / lib文件夾復制到WEB-INF / lib。
現在,將應用程序部署在Tomcat服務器上。
您不需要像使用EndpointPublisher那樣由我們自己發布服務。
Tomcat啟動并運行后,請在http:// localhost:8080 / CAAS / services / AuthenticationService?wsdl中查看生成的wsdl。
現在,如果您使用獨立客戶端測試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開發WebServices和在Tomcat-6上部署JAX-WS WebService的信息 。
翻譯自: https://www.javacodegeeks.com/2012/03/web-services-with-jax-ws-on-tomcat.html