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接口

 1 import java.util.Date;
 2 import java.util.List;
 3 
 4 import javax.jws.WebMethod;
 5 import javax.jws.WebParam;
 6 import javax.jws.WebResult;
 7 import javax.jws.WebService;
 8 
 9 import demo.axis2.jaxws.model.Notice;
10 
11 @WebService(targetNamespace = "http://core.jaxws.axis2.demo/notice")
12 public interface NoticeBusiness {
13     /**
14      * 根据编号查询通知信息
15      * 
16      * @param noticeId
17      * @return
18      */
19     @WebMethod(action = "getByNoticeId")
20     @WebResult(name = "notice")
21     Notice getByNoticeId(@WebParam(name = "informationId") Integer noticeId);
22 
23     /**
24      * 根据供应商编号,状态,发布日期查询通知信息
25      * 
26      * @param supId
27      * @param status
28      * @param releaseTime
29      * @return
30      */
31     @WebMethod(action = "queryNotices")
32     @WebResult(name = "notices")
33     List<Notice> queryNotices(@WebParam(name = "supId") Integer supId,
34             @WebParam(name = "status") Integer status,
35             @WebParam(name = "releaseTime") Date releaseTime);
36 }

 

2)创建通知接口的实现类

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 import javax.jws.WebService;
 7 
 8 import demo.axis2.jaxws.core.NoticeBusiness;
 9 import demo.axis2.jaxws.dao.NoticeDAO;
10 import demo.axis2.jaxws.model.Notice;
11 
12 @WebService(endpointInterface = "demo.axis2.jaxws.core.NoticeBusiness", serviceName = "Notice")
13 public class NoticeBusinessImpl implements NoticeBusiness {
14 
15     @Override
16     public Notice getByNoticeId(Integer noticeId) {
17         Notice notice = NoticeDAO.instance.getModel().get(noticeId);
18         if (notice == null)
19             throw new RuntimeException("Notice with " + noticeId + " not found");
20         return notice;
21     }
22 
23     @Override
24     public List<Notice> queryNotices(Integer supId, Integer status, Date releaseTime) {
25         List<Notice> noticeList = new ArrayList<Notice>();
26         Collection<Notice> notices = NoticeDAO.instance.getModel().values();
27         for (Notice notice : notices) {
28             if (notice.getSupId().intValue() == supId.intValue()
29                     && notice.getStatus().intValue() == status.intValue()
30                     && notice.getReleaseTime().after(releaseTime)) {
31                 noticeList.add(notice);
32             }
33         }
34 
35         if (noticeList.size() == 0)
36             throw new RuntimeException("Notice not found");
37         return noticeList;
38     }
39 }

 

3)辅助类NoticeDAO,此类为枚举类型,用于提供测试数据

 1 import java.util.Date;
 2 import java.util.HashMap;
 3 import java.util.Map;
 4 
 5 import demo.axis2.jaxws.model.Notice;
 6 
 7 public enum NoticeDAO {
 8     instance;
 9 
10     private Map<Integer, Notice> notices = new HashMap<Integer, Notice>();
11 
12     private NoticeDAO() {
13         notices.put(1, new Notice(1, 10000, "51 holiday notice", "Fifty-one will leave three days",
14                 "/images/20120701101010111.jpg", Constants.NOTICE_STATUS_RELEASED, "admin",
15                 new Date()));
16         notices.put(2, new Notice(2, 10000, "Mid notice",
17                 "Mid-Autumn Festival , National Day holiday 8 days",
18                 "/images/20120701101010222.jpg", Constants.NOTICE_STATUS_RELEASED, "admin",
19                 new Date()));
20     }
21 
22     public Map<Integer, Notice> getModel() {
23         return notices;
24     }
25 }

 

4)实体类,由于篇幅的原因,代码未给数setter,getter方法

 1 public class Notice implements Serializable {
 2 
 3     private static final long serialVersionUID = 1L;
 4     private Integer noticeId;
 5     private Integer supId;
 6     private String title;
 7     private String content;
 8     private String attachment;
 9     private Integer status;
10     protected String releaseUser;
11     protected Date releaseTime;
12 
13     public Notice() {
14 
15     }
16 
17     public Notice(Integer noticeId, Integer supId, String title, String content, String attachment,
18             Integer status, String releaseUser, Date releaseTime) {
19         super();
20         this.noticeId = noticeId;
21         this.supId = supId;
22         this.title = title;
23         this.content = content;
24         this.attachment = attachment;
25         this.status = status;
26         this.releaseUser = releaseUser;
27         this.releaseTime = releaseTime;
28     }
29 }

 

5)常量类

public class Constants {
    /** 通知状态 */
    public static final int NOTICE_STATUS_COMMITED = 0;// 已提交
    public static final int NOTICE_STATUS_RELEASED = 1;// 已发布
}

 

6)修改web.xml文件,增加如下内容

 1 <servlet>
 2     <servlet-name>AxisServlet</servlet-name>
 3     <display-name>Apache-Axis Servlet</display-name>
 4     <servlet-class>
 5         org.apache.axis2.transport.http.AxisServlet</servlet-class>
 6     <load-on-startup>1</load-on-startup>
 7 </servlet>
 8 
 9 <servlet-mapping>
10     <servlet-name>AxisServlet</servlet-name>
11     <url-pattern>/services/*</url-pattern>
12 </servlet-mapping>

 

另外特别需要注意的是,需要把axis2中的axis2.xml文件至于WEB-INF目录下

4. maven的项目管理文件pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3 <modelVersion>4.0.0</modelVersion>
  4 <groupId>demo.axis2.jaxws</groupId>
  5 <artifactId>demo.axis2.jaxws</artifactId>
  6 <packaging>war</packaging>
  7 <version>1.0</version>
  8 <name>demo.axis2.jaxws Maven Webapp</name>
  9 <url>http://maven.apache.org</url>
 10 <dependencies>
 11     <dependency>
 12         <groupId>javax.servlet</groupId>
 13         <artifactId>servlet-api</artifactId>
 14         <version>2.4</version>
 15     </dependency>
 16     <dependency>
 17         <groupId>org.apache.axis2</groupId>
 18         <artifactId>axis2-kernel</artifactId>
 19         <version>1.6.2</version>
 20     </dependency>
 21     <dependency>
 22         <groupId>org.apache.axis2</groupId>
 23         <artifactId>axis2-jaxws</artifactId>
 24         <version>1.6.2</version>
 25     </dependency>
 26     <dependency>
 27         <groupId>org.apache.axis2</groupId>
 28         <artifactId>axis2-codegen</artifactId>
 29         <version>1.6.2</version>
 30     </dependency>
 31     <dependency>
 32         <groupId>org.apache.axis2</groupId>
 33         <artifactId>axis2-adb</artifactId>
 34         <version>1.6.2</version>
 35     </dependency>
 36     <dependency>
 37         <groupId>org.apache.axis2</groupId>
 38         <artifactId>axis2-transport-local</artifactId>
 39         <version>1.6.2</version>
 40     </dependency>
 41     <dependency>
 42         <groupId>org.apache.axis2</groupId>
 43         <artifactId>addressing</artifactId>
 44         <version>1.6.2</version>
 45         <type>mar</type>
 46     </dependency>
 47     <dependency>
 48         <groupId>com.sun.xml.ws</groupId>
 49         <artifactId>jaxws-rt</artifactId>
 50         <version>2.1.3</version>
 51         <exclusions>
 52             <exclusion>
 53                 <groupId>javax.xml.ws</groupId>
 54                 <artifactId>jaxws-api</artifactId>
 55             </exclusion>
 56             <exclusion>
 57                 <groupId>com.sun.xml.bind</groupId>
 58                 <artifactId>jaxb-impl</artifactId>
 59             </exclusion>
 60             <exclusion>
 61                 <groupId>com.sun.xml.messaging.saaj</groupId>
 62                 <artifactId>saaj-impl</artifactId>
 63             </exclusion>
 64             <exclusion>
 65                 <groupId>com.sun.xml.stream.buffer</groupId>
 66                 <artifactId>streambuffer</artifactId>
 67             </exclusion>
 68             <exclusion>
 69                 <groupId>com.sun.xml.stream</groupId>
 70                 <artifactId>sjsxp</artifactId>
 71             </exclusion>
 72             <exclusion>
 73                 <groupId>org.jvnet.staxex</groupId>
 74                 <artifactId>stax-ex</artifactId>
 75             </exclusion>
 76             <exclusion>
 77                 <groupId>com.sun.org.apache.xml.internal</groupId>
 78                 <artifactId>resolver</artifactId>
 79             </exclusion>
 80             <exclusion>
 81                 <groupId>org.jvnet</groupId>
 82                 <artifactId>mimepull</artifactId>
 83             </exclusion>
 84         </exclusions>
 85     </dependency>
 86     <dependency>
 87         <groupId>commons-logging</groupId>
 88         <artifactId>commons-logging</artifactId>
 89         <version>1.1.2</version>
 90     </dependency>
 91     <dependency>
 92         <groupId>log4j</groupId>
 93         <artifactId>log4j</artifactId>
 94         <version>1.2.17</version>
 95     </dependency>
 96     <dependency>
 97         <groupId>commons-logging</groupId>
 98         <artifactId>commons-logging</artifactId>
 99         <version>1.1.2</version>
100     </dependency>
101     <dependency>
102         <groupId>commons-lang</groupId>
103         <artifactId>commons-lang</artifactId>
104         <version>2.6</version>
105     </dependency>
106     <dependency>
107         <groupId>junit</groupId>
108         <artifactId>junit</artifactId>
109         <version>4.10</version>
110         <scope>test</scope>
111     </dependency>
112 </dependencies>
113 <build>
114     <finalName>demo.axis2.jaxws</finalName>
115     <plugins>
116         <plugin>
117             <groupId>org.apache.maven.plugins</groupId>
118             <artifactId>maven-compiler-plugin</artifactId>
119             <configuration>
120                 <source>1.6</source>
121                 <target>1.6</target>
122             </configuration>
123         </plugin>
124     </plugins>
125     <resources>
126         <resource>
127             <directory>src/main/resources</directory>
128             <includes>
129                 <include>**/*</include>
130             </includes>
131         </resource>
132         <resource>
133             <directory>src/main/java</directory>
134             <includes>
135                 <include>**/*.xml</include>
136             </includes>
137         </resource>
138     </resources>
139 </build>
140 </project>

 

5. 发布程序到tomcat或其他J2EE服务器,启动服务器,就可在浏览器中通过如下URL,http://localhost/demo.axis2.jaxws/services/Notice?wsdl ,看到services定义了,说明服务发布成功

6. 客户端访问服务代码

 1 import org.apache.axiom.om.OMAbstractFactory;
 2 import org.apache.axiom.om.OMElement;
 3 import org.apache.axiom.om.OMFactory;
 4 import org.apache.axiom.om.OMNamespace;
 5 import org.apache.axis2.Constants;
 6 import org.apache.axis2.addressing.EndpointReference;
 7 import org.apache.axis2.client.Options;
 8 import org.apache.axis2.client.ServiceClient;
 9 
10 public class NoticeClient {
11     private static EndpointReference targetEPR = new EndpointReference(
12             "http://localhost/demo.axis2.jaxws/services/Notice");
13 
14     public static OMElement getByNoticeId(Integer noticeId) {
15         OMFactory fac = OMAbstractFactory.getOMFactory();
16         OMNamespace omNs = fac.createOMNamespace("http://core.jaxws.axis2.demo/", "tns");
17 
18         OMElement method = fac.createOMElement("getByNoticeId", omNs);
19         OMElement value = fac.createOMElement("noticeId", omNs);
20         value.addChild(fac.createOMText(value, noticeId.toString()));
21         method.addChild(value);
22         return method;
23     }
24 
25     public static OMElement queryNotices(Integer supId, Integer status, String releaseTime) {
26         OMFactory fac = OMAbstractFactory.getOMFactory();
27         OMNamespace omNs = fac.createOMNamespace("http://core.jaxws.axis2.demo/", "tns");
28 
29         OMElement method = fac.createOMElement("queryNotices", omNs);
30 
31         OMElement value = fac.createOMElement("supId", omNs);
32         value.addChild(fac.createOMText(value, supId.toString()));
33         method.addChild(value);
34 
35         value = fac.createOMElement("status", omNs);
36         value.addChild(fac.createOMText(value, status.toString()));
37         method.addChild(value);
38 
39         value = fac.createOMElement("releaseTime", omNs);
40         value.addChild(fac.createOMText(value, releaseTime));
41         method.addChild(value);
42 
43         return method;
44     }
45 
46     public static void main(String[] args) {
47         try {
48             Options options = new Options();
49             options.setTo(targetEPR);
50             options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
51 
52             ServiceClient sender = new ServiceClient();
53             sender.setOptions(options);
54 
55             System.out.println("getByActivityId start");
56             OMElement result = sender.sendReceive(getByNoticeId(1));
57             System.out.println(result.toString());
58 
59             System.out.println("queryByReleaseTime start!");
60             result = sender.sendReceive(queryNotices(10000, 1, "2013-07-03T16:07:21+08:00"));
61             System.out.println(result.toString());
62         } catch (Exception e) {
63             e.printStackTrace();
64         }
65     }
66 }

 

posted @ 2014-07-06 23:11  褐色礁石  阅读(1223)  评论(0编辑  收藏  举报