Tomcat上具有JAX-WS的Web服務

讓我們假設一家企業正在一個集中式系統中維護用戶身份驗證詳細信息。 我們需要創建一個AuthenticationService,它將獲取憑據,對其進行驗證并返回狀態。 其余的應用程序將使用AuthenticationService對用戶進行身份驗證。







創建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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373504.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373504.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373504.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python的下載及安裝

1、官網下載地址&#xff1a;https://www.python.org/downloads/ 2、python設置環境變量&#xff1a; 在系統變量里添加Python的安裝位置 3、在cmd里輸入python里即可轉載于:https://www.cnblogs.com/fun0623/p/5257573.html

MongoDB 字段拼接 $concat(aggregation)

$concat 拼接字符串操作&#xff0c;返回拼接后的字符串。語法格式如下&#xff1a; { $concat: [ <expression1>, <expression2>, ... ] }參數可以是任何有效的表達式&#xff0c;只要它們解析為字符串即可。 有關表達式的更多信息&#xff0c;請參閱表達式。 示…

cmake mysql 編譯參數_Cmake-MySQL編譯參數說明

Cmake-MySQL編譯參數說明(來源于MySQL官方手冊)https://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.htmlFormats Description DefaultIntroduced Removed ##格式描述默認導入刪除BUILD_CONFIG Use same build options as official releases ##b使用相同…

動態給H5頁面綁定數據,基本萬能無錯誤!

此為原創&#xff0c;轉載請注明出處&#xff01; /* * 共通用綁定頁面數據用方法 * * param bingData 需要綁定的數據 * * return 無 * */function commonBindData(bingData) { // 取得需綁定的json數據 var jsonArray eval("(" bingData ")"); // …

c語言函數---M

書畫小說軟件 制作更滿意的讀、更舒心的寫、更輕松的公布最全古典小說網 由本軟件公布所得main()主函數 每一C 程序都必須有一main()函數, 能夠依據自己的愛好把它放在程序的某 個地方。有些程序猿把它放在最前面, 而還有一些程序猿把它放在最后面, 不管放 在哪個地方, 下面幾…

使用Apache ActiveMQ的JMS開發基礎

去年是我嘗試JMS的時候。 背后的想法和概念讓我有些困惑&#xff0c;但是當我知道它的用途后&#xff0c;我很快就掌握了它。 在本文中&#xff0c;我將展示使用Apache ActiveMQ作為后端使用Java開發簡單的生產者/消費者的基礎。 讓我們首先從概念開始&#xff0c;這是一個簡單…

vijos p1460——拉力賽

描述 車展結束后&#xff0c;游樂園決定舉辦一次盛大的山道拉力賽&#xff0c;平平和韻韻自然也要來參加大賽。 賽場上共有n個連通的計時點&#xff0c;n-1條賽道&#xff08;構成了一棵樹&#xff09;。每個計時點的高度都不相同&#xff08;父結點的高度必然大于子結點&#…

mysql acid_Mysql中ACID的原理

原子性 (Atomicity)原子性是指一個事務是一個不可分割的工作單位&#xff0c;其中的操作要么都做&#xff0c;要么都不做。隔離性 (Isolation)隔離性是指多個事務并發執行的時候&#xff0c;事務內部的操作與其他事務是隔離的&#xff0c;并發執行的各個事務之間不能互相干擾…

MongoDB 自動刪除集合中過期的數據——TTL索引

簡介 ? TTL (Time To Live, 有生命周期的) 索引是特殊單字段索引&#xff0c;MongoDB可以用來在一定時間后自動從集合中刪除文檔的特殊索引。 這對于某些類型的數據非常好&#xff0c;例如機器生成的事件數據&#xff0c;日志和會話信息&#xff0c;這些信息只需要在數據庫中…

PLSQL 經常自動斷開失去連接的解決過程

問題背景&#xff1a; 情況是這樣的&#xff0c;很多開發同事的PLSQL上班時間開著8個小時&#xff0c;有時候他們出去抽煙后或者中午吃完飯&#xff0c;回來在PLSQL上面執行就報錯無響應&#xff0c;然后卡住了半天動彈不了&#xff0c;非得重新登錄plsql才生效&#xff0c;我猜…

使用Cobertura,JUnit,HSQLDB,JPA涵蓋您的測試

你好&#xff01;你好嗎&#xff1f; 今天讓我們談談一個非常有用的工具&#xff0c;名為“ Cobertura”。 該框架與我們在另一篇文章中看到的Emma框架具有相同的功能。 Cobertura和Emma之間的主要區別在于Cobertura顯示帶有圖形的簡歷頁面。 如果要查看有關該主題的其他主題…

fedora mysql gui_fedora8安裝 mysql++失敗!!裝了一個晚上沒搞定!!傷心阿!

fedora8安裝 mysql失敗&#xff01;&#xff01;裝了一個晚上沒搞定&#xff01;&#xff01;傷心阿&#xff01;發布時間:2008-02-24 05:15:27來源:紅聯作者:lygzx[rootF8 mysql-3.0.0]# ./configure --w/usr/lib/mysqlconfigure: error: unrecognized option: --w/usr/lib/my…

MongoDB 數組類型查詢 —— $elemMatch 操作符

描述 $elemMatch 數組查詢操作用于查詢數組值中至少有一個能完全匹配所有的查詢條件的文檔。語法格式如下&#xff1a; { <field>: { $elemMatch: { <query1>, <query2>, ... } } }如果只有一個查詢條件就沒必要使用 $elemMatch。 限制 不能指定 $where 查…

MVC4 Action 方法的執行

1. ActionInvoker 的執行&#xff1a; 在MVC 中 包括Model綁定與驗證在內的整個Action的執行是通過一個名為ActionInvoker的組件來完成的。 它同樣具有 同步/異步兩個版本。 分別實現了接口 IActionInvoker /IAsyncActionInvoker。 ASP.NET MVC 中真正用于Action方法同步和異步…

C# 基礎知識總結

要學好C#&#xff0c;基礎知識的重要性不言而喻&#xff0c;現將常用到的一些基礎進行總結&#xff0c;總結如下&#xff1a; 01. 數據類型轉換&#xff1a; 強制類型轉換(Chart--> int): char crA; int i (int)(cr); 02. 委托/匿名函數/Lamda表達式&#xff1a; 委托是匿…

Java注釋和真實世界的Spring示例

“注釋”是編程語言定義的一種&#xff0c;用作“標記”。 可以將它們視為編程語言引擎可以理解的注釋行。 它們不會直接影響程序的執行&#xff0c;但是會在需要時間接影響。 定義 注釋使用interface關鍵字定義&#xff0c;并且與接口相似。 它具有定義類似于接口方法的屬性。…

scrapy+mysql+pipeline+更新數據_python3+Scrapy爬蟲實戰(二)—— 使用pipeline數據保存到文本和數據庫(mysql)...

前言保存本地存儲Json數據配置setting保存數據庫創建數據庫創建表編寫pipelines配置setting本文是對上篇文章所講的代碼進一步優化&#xff0c;回看可以點這里&#xff0c;代碼就直接在上一篇代碼中進行改造&#xff0c;沒有的小伙伴可以在這里下載。前言Scrapy 提供了 pipelin…

NYOJ 44 子串和

子串和 時間限制&#xff1a;5000 ms | 內存限制&#xff1a;65535 KB難度&#xff1a;3描述 給定一整型數列{a1,a2...,an}&#xff0c;找出連續非空子串{ax,ax1,...,ay}&#xff0c;使得該子序列的和最大&#xff0c;其中&#xff0c;1<x<y<n。 輸入 第一行是一個…

學習進度條

學習進度條 周次 學習時間 新編寫代碼行數 博客量&#xff08;篇&#xff09; 學到知識點 第一周 160 0 1 github的使用和認識軟件工程這門課的價值。 第二周 160 130 3 復利的計算和Github的一些簡單操作還有就是進行項目的開發分析&#xff0c;還有就是對…

ARM基礎

1.  將32位a的【7&#xff1a;4】改成0101 -> a a&(~(0xF << 4)) | (0x5 << 4)&#xff1b; 2.  32位&#xff1a;單次處理數據32位。 3.  對于CPU而言&#xff0c;一切皆內存&#xff1b; 4.  DMA總線&#xff1a;不經過CPU直接在內存和內存間交換…