axis2实践(一)JAX-WS入门示例
1. 实例说明
现在大多数的网站都有通知功能(例如,放假通知,网站维护通知等),本实例就是针对于通知,发布两个WebService服务
1)根据供应商编号,状态,发布日期查询通知信息
2)根据编号查询通知信息
特别是需要注意的是本实例用到的axis2的版本为1.6.2,JDK版本为1.6
2. JAX-WS常用注解
javax.jws.WebService
@WebService 注释标记Java 类为标记服务端点接口(SEI) --targetNamespace,指定从 Web Service 生成的 WSDL 和 XML 元素的 XML 名称空间。缺省值为从包含该 Web Service 的包名映射的名称空间。(字符串)
javax.jws.WebMethod
@WebMethod 注释表示作为一项 Web Service 操作的方法。 --action,定义此操作的行为。对于 SOAP 绑定,此值将确定 SOAPAction 头的值。缺省值为 Java 方法的名称。(字符串)
javax.jws.WebParam
@WebParam 注释用于定制从单个参数至 Web Service 消息部件和 XML 元素的映射。
javax.jws.WebResult
@WebResult 注释用于定制从返回值至 WSDL 部件或 XML 元素的映射。将此注释应用于客户机或服务器服务端点接口(SEI)上的方法,或者应用于 JavaBeans 端点的服务器端点实现类。
3. 创建WebService服务
1)根据面向接口编程的原则,先创建一个Notice接口
import java.util.Date;
import java.util.List; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; import demo.axis2.jaxws.model.Notice; @WebService(targetNamespace = "http://core.jaxws.axis2.demo/notice")
public interface NoticeBusiness {
/**
* 根据编号查询通知信息
*
* @param noticeId
* @return
*/
@WebMethod(action = "getByNoticeId")
@WebResult(name = "notice")
Notice getByNoticeId(@WebParam(name = "informationId") Integer noticeId); /**
* 根据供应商编号,状态,发布日期查询通知信息
*
* @param supId
* @param status
* @param releaseTime
* @return
*/
@WebMethod(action = "queryNotices")
@WebResult(name = "notices")
List<Notice> queryNotices(@WebParam(name = "supId") Integer supId,
@WebParam(name = "status") Integer status,
@WebParam(name = "releaseTime") Date releaseTime);
}
2)创建通知接口的实现类
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List; import javax.jws.WebService; import demo.axis2.jaxws.core.NoticeBusiness;
import demo.axis2.jaxws.dao.NoticeDAO;
import demo.axis2.jaxws.model.Notice; @WebService(endpointInterface = "demo.axis2.jaxws.core.NoticeBusiness", serviceName = "Notice")
public class NoticeBusinessImpl implements NoticeBusiness { @Override
public Notice getByNoticeId(Integer noticeId) {
Notice notice = NoticeDAO.instance.getModel().get(noticeId);
if (notice == null)
throw new RuntimeException("Notice with " + noticeId + " not found");
return notice;
} @Override
public List<Notice> queryNotices(Integer supId, Integer status, Date releaseTime) {
List<Notice> noticeList = new ArrayList<Notice>();
Collection<Notice> notices = NoticeDAO.instance.getModel().values();
for (Notice notice : notices) {
if (notice.getSupId().intValue() == supId.intValue()
&& notice.getStatus().intValue() == status.intValue()
&& notice.getReleaseTime().after(releaseTime)) {
noticeList.add(notice);
}
} if (noticeList.size() == 0)
throw new RuntimeException("Notice not found");
return noticeList;
}
}
3)辅助类NoticeDAO,此类为枚举类型,用于提供测试数据
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import demo.axis2.jaxws.model.Notice; public enum NoticeDAO {
instance; private Map<Integer, Notice> notices = new HashMap<Integer, Notice>(); private NoticeDAO() {
notices.put(1, new Notice(1, 10000, "51 holiday notice", "Fifty-one will leave three days",
"/images/20120701101010111.jpg", Constants.NOTICE_STATUS_RELEASED, "admin",
new Date()));
notices.put(2, new Notice(2, 10000, "Mid notice",
"Mid-Autumn Festival , National Day holiday 8 days",
"/images/20120701101010222.jpg", Constants.NOTICE_STATUS_RELEASED, "admin",
new Date()));
} public Map<Integer, Notice> getModel() {
return notices;
}
}
4)实体类,由于篇幅的原因,代码未给数setter,getter方法
public class Notice implements Serializable {
private static final long serialVersionUID = 1L;
private Integer noticeId;
private Integer supId;
private String title;
private String content;
private String attachment;
private Integer status;
protected String releaseUser;
protected Date releaseTime;
public Notice() {
}
public Notice(Integer noticeId, Integer supId, String title, String content, String attachment,
Integer status, String releaseUser, Date releaseTime) {
super();
this.noticeId = noticeId;
this.supId = supId;
this.title = title;
this.content = content;
this.attachment = attachment;
this.status = status;
this.releaseUser = releaseUser;
this.releaseTime = releaseTime;
}
}
5)常量类
public class Constants {
/** 通知状态 */
public static final int NOTICE_STATUS_COMMITED = 0;// 已提交
public static final int NOTICE_STATUS_RELEASED = 1;// 已发布
}
6)修改web.xml文件,增加如下内容
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
另外特别需要注意的是,需要把axis2中的axis2.xml文件至于WEB-INF目录下
4. maven的项目管理文件pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo.axis2.jaxws</groupId>
<artifactId>demo.axis2.jaxws</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>demo.axis2.jaxws Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-jaxws</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-codegen</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>addressing</artifactId>
<version>1.6.2</version>
<type>mar</type>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.stream</groupId>
<artifactId>sjsxp</artifactId>
</exclusion>
<exclusion>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.org.apache.xml.internal</groupId>
<artifactId>resolver</artifactId>
</exclusion>
<exclusion>
<groupId>org.jvnet</groupId>
<artifactId>mimepull</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>demo.axis2.jaxws</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
5. 发布程序到tomcat或其他J2EE服务器,启动服务器,就可在浏览器中通过如下URL,http://localhost/demo.axis2.jaxws/services/Notice?wsdl ,看到services定义了,说明服务发布成功
6. 客户端访问服务代码
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient; public class NoticeClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost/demo.axis2.jaxws/services/Notice"); public static OMElement getByNoticeId(Integer noticeId) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://core.jaxws.axis2.demo/", "tns"); OMElement method = fac.createOMElement("getByNoticeId", omNs);
OMElement value = fac.createOMElement("noticeId", omNs);
value.addChild(fac.createOMText(value, noticeId.toString()));
method.addChild(value);
return method;
} public static OMElement queryNotices(Integer supId, Integer status, String releaseTime) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://core.jaxws.axis2.demo/", "tns"); OMElement method = fac.createOMElement("queryNotices", omNs); OMElement value = fac.createOMElement("supId", omNs);
value.addChild(fac.createOMText(value, supId.toString()));
method.addChild(value); value = fac.createOMElement("status", omNs);
value.addChild(fac.createOMText(value, status.toString()));
method.addChild(value); value = fac.createOMElement("releaseTime", omNs);
value.addChild(fac.createOMText(value, releaseTime));
method.addChild(value); return method;
} public static void main(String[] args) {
try {
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient();
sender.setOptions(options); System.out.println("getByActivityId start");
OMElement result = sender.sendReceive(getByNoticeId(1));
System.out.println(result.toString()); System.out.println("queryByReleaseTime start!");
result = sender.sendReceive(queryNotices(10000, 1, "2013-07-03T16:07:21+08:00"));
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
axis2实践(一)JAX-WS入门示例的更多相关文章
- Dubbo实践(一)入门示例
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候 ...
- [WCF编程]1.WCF入门示例
一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...
- Web Service简单入门示例
Web Service简单入门示例 我们一般实现Web Service的方法有非常多种.当中我主要使用了CXF Apache插件和Axis 2两种. Web Service是应用服务商为了解决 ...
- Maven入门示例(3):自动部署至外部Tomcat
Maven入门示例(3):自动部署至外部Tomcat 博客分类: maven 2012原创 Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...
- 1.【转】spring MVC入门示例(hello world demo)
1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- Couchbase之个人描述及入门示例
本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...
- Velocity魔法堂系列一:入门示例
一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...
- OUYA游戏开发核心技术剖析OUYA游戏入门示例——StarterKit
第1章 OUYA游戏入门示例——StarterKit StarterKit是一个多场景的游戏示例,也是OUYA官方推荐给入门开发者分析的第一个完整游戏示例.本章会对StarterKit做详细介绍,包 ...
随机推荐
- linux系统中 redis 保存数据的5种形式 linux后端模式启动 jedis无法通过IP地址和端口号访问如何修改linux防火墙
vim修改redis.conf配置文件(我的已经复制到虚拟机的/usr/local/redis/bin目录下)为daemonize yes, 以后端模式启动 ./redis-server redis. ...
- Java分享笔记:使用keySet方法获取Map集合中的元素
/*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...
- python__基础 : 类的__new__方法与实现一个单例
__new__ : 这个方法的作用主要是创建一个实例,在创建实例时首先会调用 __new__方法 ,然后调用__init__对实例进行初始化, 如果想修改 __new__ 这个方法,那么最后要 ret ...
- ThinkPHP路由去掉隐藏URL中的index.php
官方默认的.htaccess文件 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On ...
- windows下的node.js和npm的安装步骤详解
一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...
- <Docker学习>3. docker镜像命令使用
镜像提供容器运行时所需要的程序,资源.配置文件等,是一个特殊的文件系统.是容器运行的基础.镜像是多层文件系统组成的,是一个分层存储的架构,在镜像的构建中,会一层层的构建,每一层构建完成就不会发生改变, ...
- Android 使用Retrofit2.0+OkHttp3.0实现缓存处理+Cookie持久化第三方库
1.Retrofit+OkHttp的缓存机制 1.1.第一点 在响应请求之后在 data/data/<包名>/cache 下建立一个response 文件夹,保存缓存数据. 1.2.第二点 ...
- is 和 == 的区别,utf和gbk的转换,join用法
is 和 == 的区别 # is 比较的是内存地址 # == 比较的是值 a = 'alex' b = 'alex' #int,str(小数据池)会被缓存,为了节约内存 print(id(a),id( ...
- 1 Mongodb安装
1.NoSQL简介 NoSQL,全名Not Only SQL,指的是非关系型的数据库 随着访问量的上升,网站的数据库性能出现了问题,于是NoSQL被设计出来了 优点.缺点 优点 高扩展性 分布式计算 ...
- Android log 引发的血案
今天调试代码,我打印了一个东西: Log.d("WelcomeActivity", res.str); 结果总是代码执行不到这一行的下一行,程序也没有挂掉.后来,我自己去想各种可能 ...