Tomcat上具有JAX-WS的Web服务
创建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
Tomcat上具有JAX-WS的Web服务相关推荐
- web服务器之iis,apache,tomcat三者之间的比较
IIS-Apache-Tomcat的区别 IIS与Tomcat的区别 IIS是微软公司的Web服务器.主要支持ASP语言环境. Tomcat是Java Servlet 2.2和JavaServer ...
- 常用的系统架构 web服务器之iis,apache,tomcat三者之间的比较
常用的系统架构是: Linux + Apache + PHP + MySQL Linux + Apache + Java (WebSphere) + Oracle Windows Server 200 ...
- Linux实战教学笔记37:企业级Nginx Web服务优化实战(上)
一,Nginx基本安全优化 1.1 调整参数隐藏Nginx软件版本号信息 一般来说,软件的漏洞都和版本有关,这个很像汽车的缺陷,同一批次的要有问题就都有问题,别的批次可能就都是好的.因此,我们应尽量隐 ...
- 将Java服务公开为Web服务
本教程解决了开发人员面临的最实际的情况. 大多数时候,我们可能需要将某些现有服务公开为Web服务. 在项目生命周期的不同阶段可能会遇到这种情况. 如果这是初始阶段,那么您几乎是安全的,您可以为此做好充 ...
- 使用BeetleX网关部署第三方Web服务
BeetleX的http/ws网关在早期版本可以启动和管理第三方Web服务进程,在最新的1.5版本中引入了文件管理功能,通过这一功能可以对第三方Web服务进行发布管理. 加入文件管理后BeetleX的 ...
- jboss4 java_JBoss核心Java Web服务
jboss4 java 这篇博客文章涉及Web服务. 好吧,更确切地说,它处理JBoss上的"普通" java Web服务. 这意味着我们将创建一个没有任何其他框架(例如CXF,A ...
- JBoss核心Java Web服务
这篇博客文章涉及Web服务. 好吧,更确切地说,它处理JBoss上的"普通" java Web服务. 这意味着我们将创建一个没有任何其他框架(如CXF,Axis等)的Web服务. ...
- 感谢在俄勒冈州Develo的SAOs软件协会参加我的演讲“音乐背后的Web服务”的所有人...
Thanks to everyone who attended my talk "Web Services: Behind the Music" at the SAO's (Sof ...
- 使用ASP.Net 3.5 的Ajax与Web服务开发实例
本文继续介绍使用ASP.NET3.5中的AJAX环境中如何从客户端JavaScript调用Web服务方法.编写本文的目的在于让大家深刻了解基于ASP.Net3.5的Ajax和Web的服务,虽然例子比较 ...
- RESTful Web 服务 - 介绍
什么是 REST? REST 是 REpresentational State Transfer 的缩写.REST 是一种基于 Web 标准的软件架构,它使用 HTTP 协议处理数据通信.它以资源为中 ...
最新文章
- php+jq+添加css,jquery如何添加css样式?
- 分段函数是不是一定初等函数_查漏补缺问题64:一个含多参数分段函数的连续性与可导性讨论...
- python 条形图图注怎么集中注意力_如何用每个条形图的总和(Matplotlib)注释堆积条形图?...
- Spring boot集成spring-boot-starter-data-jpa环境搭建
- Latex与VSCode环境搭建问题解决
- 计算机驱动空间不够,Win8.1系统如何释放驱动器空间解决可用空间不足问题
- OpenCV Mat主要用法(1)
- [ACTF2020 新生赛]Include
- 实战演练:如何用BBED恢复删除的数据
- 【华为云技术分享】MongoDB经典故障系列五:sharding集群执行sh.stopBalancer()命令被卡住怎么办?
- DeFi 协议 Benchmark Protocol 启动第二阶段流动性挖矿计划 The Press
- 2022年7月深圳地区CPDA数据分析师认证
- 华为交换机Hybird 与 单臂路由
- Tableau 南丁格尔玫瑰图
- 打开本地html加载网页慢,浏览器打开网页很慢怎么回事_浏览器打开网页很慢如何解决...
- 配置java win10_win10 Java14安装及配置
- 双android系统pcb整合,基于Android的大屏幕拼接显示系统研究与实现
- 第五章 欧洲科技文明的起源
- 【深入理解计算机系统-学习笔记】第一章 计算机系统漫游
- 【遍历csv文件按年份统计各列个数并批量输出】
热门文章
- JDK8的日期时间类2
- java 读取 文本块_Java 13:文本块
- 骆驼(camel)命名法_Apache Camel 3 –骆驼核心vs骆驼核心引擎(较小的核心)
- maven依赖范围_Maven依赖范围
- corda_使用Spring WebFlux从Corda节点流式传输数据
- spring mvc教程_Spring MVC教程
- 过滤器过滤特定的url_如何从过滤器中排除URL
- ibm liberty_使用Eclipse和Open Liberty的Java EE 8上的Java 9
- 应行家算法_一些行家技巧和窍门
- k8s中graphite_在Graphite中存储Hystrix的几个月历史指标