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做详细介绍,包 ...
随机推荐
- input宽度超出
设置样式:style=“width:100%”;即可
- 7-1 python 操作redis
1.安装并导入redis模块 # pip install redis 安装redis模块 import redis # 导入redis模块 2.连接一个或多个redis,指定数据库名,并指定返回字符串 ...
- 【PHP】nl2br转化输出input框的换行
在input或者textarea框中输入的换行符保存到数据库是/n,如果直接输出到前端的话是不会有换行的,所以要用到nl2br转化 nl2br($test);
- 微信小程序真机定位问题技巧
小程序导航 https://wq.xmaht.top 开发者在开发小程序的时候可能会碰到一些这样的问题: 问题1 开发者工具上看效果没问题,但是在真机上测试不行? 问题2 有用户遇到小程序功能无法 ...
- spark streaming的应用
今天我们讲spark streaming的应用,这个是实时处理的,类似于Storm以及Flink相关的知识点, 说来也巧,今天的自己也去听了关于Flink的相关的讲座,可惜自己没有听得特别清楚,好像是 ...
- getElementByName????????,????????,
getElementByName可以获取多个元素,获得的是一个数组, getElementById只能获取一个,是dom从上往下的第一个元素.
- gettid 和pthread_self的区别
转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...
- runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)
每一个类对象中都一个对象方法列表(对象方法缓存) 类方法列表是存放在类对象中isa指针指向的元类对象中(类方法缓存) 方法列表中每个方法结构体中记录着方法的名称,方法实现,以及参数类型,其实selec ...
- 怎么用 copy 关键字?
NSString.NSArray.NSDictionary等等经常使用copy关键字,是因为他们有对应的可变类型:NSMutableString.NSMutableArray.NSMutableDic ...
- Djano之写api使用django_rest_framework【海瑞博客】
使用django rest framework 可以更快速和友好的编写api,当然网上有很多教程,对于高手来说相对很简单,对于新手来说,根本搞不明白.那是你没有搞明白你自己的职责,做为后端,我们只要提 ...