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();
}
}
}

最新文章

  1. sql查找去重复并且字段不为空的数据
  2. MFC覆盖OnPrepareDC实现“所见即所得”打印
  3. Asp.Net MVC4 + Oracle + EasyUI 学习 第一章
  4. 转-Apache的Order Allow,Deny 详解
  5. Nginx负载均衡配置实例详解(转)
  6. PlayMaker 学习笔记
  7. Spring中的ApplicationContext事件机制
  8. struts2中简单的文件上传
  9. 以下是关于Controller的一些Hint
  10. Extjs4.2.1中的helloworld
  11. bzoj3905: Square
  12. linux 后台运行程序
  13. 把对象列表转化成json数据格式
  14. MySQL数据引擎
  15. HighCharts之2D对数饼图
  16. IO多路复用,同步,异步,阻塞和非阻塞 区别(转)
  17. 微服务SpringCloud—Config Server对称加密
  18. trunk端口配置错误导致环路
  19. memset()函数及其作用(转)
  20. jenkins配置小结

热门文章

  1. 使用nsis开发自定义安装包使用心得,以及遇到坑
  2. 爬虫学习(十八)——selenium解决javascript渲染
  3. GNU汇编 存储器访问指令
  4. 干货!一篇文章集合所有Linux基础命令,适合所有菜鸟学习和老手回顾!
  5. dts--framework(二)
  6. 关于TP3.2框架读取Sql server中文字段数据以及处理乱码的一些小心得
  7. JDK学习---深入理解java中的LinkedList
  8. B-树 动机与结构
  9. kettle入门(三) 之kettle连接hadoop&amp;hdfs图文详解(转)
  10. python面向对象三大特性