用CXF发布Web服务

1.下载apache-cxf-2.7.6jar包,并把lib目录下的所有jar包导入项目

2.编写测试的实体类,示例如下:

 1 package cn.bd.weather.entity;
 2 
 3 import java.util.Date;
 4 
 5 import javax.xml.bind.annotation.XmlRootElement;
 6 /**
 7  * 
 8  * @author Administrator
 9  *   @XmlRootElement 表示根元素
10  */
11 @XmlRootElement(name="Weather")   //name属性默认是类名
12 public class Weather {
13     private String city;
14     private Date date;
15     private int min;
16     private int max;
17     private int average;
18     private String desc;
19     
20     public Weather() {
21         super();
22     }
23     public Weather(String city, Date date, int min, int max, int average,
24             String desc) {
25         super();
26         this.city = city;
27         this.date = date;
28         this.min = min;
29         this.max = max;
30         this.average = average;
31         this.desc = desc;
32     }
33     public String getCity() {
34         return city;
35     }
36     public void setCity(String city) {
37         this.city = city;
38     }
39     public Date getDate() {
40         return date;
41     }
42     public void setDate(Date date) {
43         this.date = date;
44     }
45     public int getMin() {
46         return min;
47     }
48     public void setMin(int min) {
49         this.min = min;
50     }
51     public int getMax() {
52         return max;
53     }
54     public void setMax(int max) {
55         this.max = max;
56     }
57     public int getAverage() {
58         return average;
59     }
60     public void setAverage(int average) {
61         this.average = average;
62     }
63     public String getDesc() {
64         return desc;
65     }
66     public void setDesc(String desc) {
67         this.desc = desc;
68     }
69     
70 }

3.编写服务接口,示例如下:

 1 package cn.bd.weather.service;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 import javax.jws.WebParam;
 6 import javax.jws.WebResult;
 7 import javax.jws.WebService;
 8 
 9 import cn.bd.weather.entity.Weather;
10 
11 @WebService(serviceName="WeatherService") //@WebService里面的targetNamespace="默认是包名"
12 public interface WeatherService {
13     @WebResult(name="out")
14     List<Weather> getWeathers(@WebParam(name="city")String city,@WebParam(name="dates")List<Date> dates);
15     //@WebParam(name="city")表示city参数的别名
16     
17 }

 

4.编写服务接口的实现,示例如下:

 1 package cn.bd.weather.service.impl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 
 6 import java.util.List;
 7 
 8 import javax.jws.WebMethod;
 9 import javax.jws.WebService;
10 
11 import cn.bd.weather.entity.Weather;
12 import cn.bd.weather.service.WeatherService;
13 
14 @WebService   //@WebService里面的targetNamespace=""
15 public class WeatherServiceImpl implements WeatherService {
16     
17     @WebMethod   //注解为web服务方法
18     public List<Weather> getWeathers(String city, List<Date> dates) {
19         List<Weather> list=new ArrayList<Weather>();
20         for(Date date : dates){
21             list.add(getWeather(city,date));
22         }
23         return list;
24     }
25     
26     @WebMethod(exclude=true)  //exclude=true 表示这个web方法不对外暴露
27     public Weather getWeather(String city,Date date){
28         return new Weather(city,date,25,27,30,"晴");
29     }
30 
31 }

 

5.编写spring容器,示例如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:jaxws="http://cxf.apache.org/jaxws"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9                         http://cxf.apache.org/jaxws
10                         http://cxf.apache.org/schemas/jaxws.xsd
11                         http://cxf.apache.org/bindings/soap
12                         http://cxf.apache.org/schemas/configuration/soap.xsd">
13     <!-- 引入CXF的文件 -->
14     <import resource="classpath:META-INF/cxf/cxf.xml"/>
15     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
16     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
17     
18     <!-- 定义发布web服务的类 -->
19     <bean id="weatherService" class="cn.bd.weather.service.impl.WeatherServiceImpl"></bean>
20     
21     <!-- 配置Endpoint   端口 -->
22     <jaxws:endpoint id="wsServiceBean" implementor="#weatherService" address="getWeather" publish="true">
23     </jaxws:endpoint>
24     
25     <!-- 
26         如何查看web服务是否了布成功
27         要在web.xml配置CXFServlet和装载spring容器
28         1.起动tomcat,在浏览器输入localhost:8090/CXFService/getWeather  
29         其中getWeather 是配置Endpoint,属性address的值
30      -->
31 </beans>

 

6.web.xml的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <context-param>
13       <param-name>contextConfigLocation</param-name>
14       <param-value>classpath:applicationContext.xml</param-value> <!-- 加载spring容器 -->
15   </context-param>
16   <listener>
17       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 监听spring容器 -->
18   </listener>
19   
20   <!-- 配置CXFServlet -->
21   <servlet>
22       <servlet-name>CXFServlet</servlet-name>
23       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
24   </servlet>
25   <servlet-mapping>
26       <servlet-name>CXFServlet</servlet-name>
27       <url-pattern>/*</url-pattern>
28   </servlet-mapping>
29   
30 </web-app>

 

posted @ 2017-04-11 08:32  写最少的代码  阅读(414)  评论(0编辑  收藏  举报