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. HDU.2111 Saving HDU(贪心)

    题目来源:Saving HDU 题意分析: XHD有个容量为v的口袋,有n个宝贝,每种宝贝的价值不一样,每种宝贝单位体积的价格也不一样,宝贝可以分割,分割后的价值和对应的体积成正比.求XHD最多能取回 ...

  2. 牛客小白月赛2 E 是是非非 【尼姆博弈】

    链接:https://www.nowcoder.com/acm/contest/86/E来源:牛客网 题目描述 坎为水,险阳失道,渊深不测:离为火,依附团结,光明绚丽. 坎卦:水洊至,习坎:君子以常德 ...

  3. MySQL工作经验

    以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...

  4. 死锁-Java代码示例

    class MyThread implements Runnable{ private Object o1 = new Object(); private Object o2 = new Object ...

  5. 深入理解restfulAPI和 Oauth2.0(精简版)

    一.restfulAPI 1.解释: restfulAPI协议,我们也可以说是一套API接口编写风格. 它被现在很多企业所认可和默认,是一套成俗的API接口编写方案. 2.restfulAPI之资源 ...

  6. [洛谷1156]垃圾陷阱(DP)

    [Luogu1156] f[i]表示高度为i时的存活时间 Code #include <cstdio> #include <algorithm> #define N 110 u ...

  7. Git的使用--将本地项目上传到Github

    Git的使用--将本地项目上传到Github 感谢作者 --> 原文链接 很早之前就注册了Github,但对其使用一直懵懵懂懂,很不熟练.直到昨天做完百度前端技术学院的task,想把代码托管到G ...

  8. hadoop,hbase,hive

    linux上安装hadoop,然后安装hbase,然后安装zookeeper,最后安装hive.hbase安装在hdfs下.hive是纯逻辑表,hbase是物理表.hdfs是hadoop上的一个组件.

  9. 16,Flask-Migrate

    终于到了Flask-Migrate,之前在学习Flask-SQLAlchemy的时候,Flask支持 makemigration / migrate 吗? 答案在这里该诉你,如果你同时拥有两个三方组件 ...

  10. UOJ #2321. 「清华集训 2017」无限之环

    首先裂点表示四个方向 一条边上都有插头或者都不有插头,相当于满足流量平衡 最大流 = 插头个数*2时有解 然后求最小费用最大流 黑白染色分别连原点汇点