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入门示例的更多相关文章

  1. Dubbo实践(一)入门示例

    dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候 ...

  2. [WCF编程]1.WCF入门示例

    一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...

  3. Web Service简单入门示例

    Web Service简单入门示例     我们一般实现Web Service的方法有非常多种.当中我主要使用了CXF Apache插件和Axis 2两种. Web Service是应用服务商为了解决 ...

  4. Maven入门示例(3):自动部署至外部Tomcat

    Maven入门示例(3):自动部署至外部Tomcat 博客分类:  maven 2012原创   Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...

  5. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  6. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  7. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  8. Couchbase之个人描述及入门示例

    本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...

  9. Velocity魔法堂系列一:入门示例

    一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...

  10. OUYA游戏开发核心技术剖析OUYA游戏入门示例——StarterKit

    第1章  OUYA游戏入门示例——StarterKit StarterKit是一个多场景的游戏示例,也是OUYA官方推荐给入门开发者分析的第一个完整游戏示例.本章会对StarterKit做详细介绍,包 ...

随机推荐

  1. POJ2409 Let it Bead(Polya定理)

    Let it Bead Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6443   Accepted: 4315 Descr ...

  2. 【杂题总汇】HDU多校赛第十场 Videos

    [HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...

  3. ssm整合-错误2

    1 警告: No mapping found for HTTP request with URI [/management] in DispatcherServlet with name 'dispa ...

  4. MySQL为何不建议使用null列

      Preface       Null is a special constraint of columns.The columns in table will be added null cons ...

  5. linux定时任务及练习

    第1章 定时任务 1.1 什么是定时任务 相当于闹钟每天叫你起床 设定一个时间去做某件事 1.2 系统定时任务 [root@zeq ~]# ll -d /etc/cron* drwxr-xr-x. 2 ...

  6. 统计寄存器AX中1 的个数

    ;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...

  7. Apache2服务配置ubuntu16.04+django1.11

    话不多说直接上步骤 环境 Ubuntu 16.04 Python 3.5.2 Django 1.11 Apache 2.4 1.Apache2安装 sudo apt-get install apach ...

  8. C# 窗口关闭事件

    首先添加一个退出事件函数 //退出按键 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogRe ...

  9. 15、python之导入模块

    一.什么是模块? 模块本质是一个py文件,我们可以通过关键字import将py文件对象导入到当前名称空间. 二.导入模块 1.import module 2.from module import ob ...

  10. contest0 from codechef

    A  CodeChef - KSPHERES 中文题意  Mandarin Chinese Eugene has a sequence of upper hemispheres and another ...