首页 > 代码库 > CXF调用web服务

CXF调用web服务

1.进入apache-cxf-2.7.6\bin目录,按住shift键,鼠标右键点击选择:  在此处打开命令窗口

2.输入 wsdl2java url ,url是对外发布的端口  ,    发布web服务示例: http://www.cnblogs.com/taobd/p/6691871.html

3.把生成的java文件复制到项目,示例如下:

  1 package cn.bd.client;
  2 import java.util.Date;
  3 import javax.xml.bind.annotation.XmlAccessType;
  4 import javax.xml.bind.annotation.XmlAccessorType;
  5 import javax.xml.bind.annotation.XmlSchemaType;
  6 import javax.xml.bind.annotation.XmlType;
  7 
  8 
  9 /**
 10  * <p>weather complex type的 Java 类。
 11  * 
 12  * <p>以下模式片段指定包含在此类中的预期内容。
 13  * 
 14  * <pre>
 15  * &lt;complexType name="weather">
 16  *   &lt;complexContent>
 17  *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 18  *       &lt;sequence>
 19  *         &lt;element name="average" type="{http://www.w3.org/2001/XMLSchema}int"/>
 20  *         &lt;element name="city" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 21  *         &lt;element name="date" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/>
 22  *         &lt;element name="desc" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 23  *         &lt;element name="max" type="{http://www.w3.org/2001/XMLSchema}int"/>
 24  *         &lt;element name="min" type="{http://www.w3.org/2001/XMLSchema}int"/>
 25  *       &lt;/sequence>
 26  *     &lt;/restriction>
 27  *   &lt;/complexContent>
 28  * &lt;/complexType>
 29  * </pre>
 30  * 
 31  * 
 32  */
 33 @XmlAccessorType(XmlAccessType.FIELD)
 34 @XmlType(name = "weather", propOrder = {
 35     "average",
 36     "city",
 37     "date",
 38     "desc",
 39     "max",
 40     "min"
 41 })
 42 public class Weather {
 43 
 44     protected int average;
 45     protected String city;
 46     @XmlSchemaType(name = "dateTime")
 47     protected Date date;
 48     protected String desc;
 49     protected int max;
 50     protected int min;
 51 
 52     /**
 53      * 获取average属性的值。
 54      * 
 55      */
 56     public int getAverage() {
 57         return average;
 58     }
 59 
 60     /**
 61      * 设置average属性的值。
 62      * 
 63      */
 64     public void setAverage(int value) {
 65         this.average = value;
 66     }
 67 
 68     /**
 69      * 获取city属性的值。
 70      * 
 71      * @return
 72      *     possible object is
 73      *     {@link String }
 74      *     
 75      */
 76     public String getCity() {
 77         return city;
 78     }
 79 
 80     /**
 81      * 设置city属性的值。
 82      * 
 83      * @param value
 84      *     allowed object is
 85      *     {@link String }
 86      *     
 87      */
 88     public void setCity(String value) {
 89         this.city = value;
 90     }
 91 
 92     /**
 93      * 获取date属性的值。
 94      * 
 95      * @return
 96      *     possible object is
 97      *     {@link XMLGregorianCalendar }
 98      *     
 99      */
100     public Date getDate() {
101         return date;
102     }
103 
104     /**
105      * 设置date属性的值。
106      * 
107      * @param value
108      *     allowed object is
109      *     {@link XMLGregorianCalendar }
110      *     
111      */
112     public void setDate(Date value) {
113         this.date = value;
114     }
115 
116     /**
117      * 获取desc属性的值。
118      * 
119      * @return
120      *     possible object is
121      *     {@link String }
122      *     
123      */
124     public String getDesc() {
125         return desc;
126     }
127 
128     /**
129      * 设置desc属性的值。
130      * 
131      * @param value
132      *     allowed object is
133      *     {@link String }
134      *     
135      */
136     public void setDesc(String value) {
137         this.desc = value;
138     }
139 
140     /**
141      * 获取max属性的值。
142      * 
143      */
144     public int getMax() {
145         return max;
146     }
147 
148     /**
149      * 设置max属性的值。
150      * 
151      */
152     public void setMax(int value) {
153         this.max = value;
154     }
155 
156     /**
157      * 获取min属性的值。
158      * 
159      */
160     public int getMin() {
161         return min;
162     }
163 
164     /**
165      * 设置min属性的值。
166      * 
167      */
168     public void setMin(int value) {
169         this.min = value;
170     }
171 
172 }
 1 package cn.bd.client;
 2 
 3 import java.util.Date;
 4 
 5 import javax.jws.WebMethod;
 6 import javax.jws.WebParam;
 7 import javax.jws.WebResult;
 8 import javax.jws.WebService;
 9 import javax.xml.bind.annotation.XmlSeeAlso;
10 import javax.xml.ws.RequestWrapper;
11 import javax.xml.ws.ResponseWrapper;
12 
13 /**
14  * This class was generated by Apache CXF 2.7.6
15  * 2017-04-10T20:28:30.443+08:00
16  * Generated source version: 2.7.6
17  * 
18  */
19 @WebService(targetNamespace = "http://service.weather.bd.cn/", name = "WeatherService")
20 public interface WeatherService {
21 
22     @WebResult(name = "out", targetNamespace = "")
23     @RequestWrapper(localName = "getWeathers", targetNamespace = "http://service.weather.bd.cn/", className = "cn.bd.weather.service.GetWeathers")
24     @WebMethod
25     @ResponseWrapper(localName = "getWeathersResponse", targetNamespace = "http://service.weather.bd.cn/", className = "cn.bd.weather.service.GetWeathersResponse")
26     public java.util.List<Weather> getWeathers(
27         @WebParam(name = "city", targetNamespace = "")
28         java.lang.String city,
29         @WebParam(name = "dates", targetNamespace = "")
30         java.util.List<Date> dates
31     );
32 }

 

 

4.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:context="http://www.springframework.org/schema/context"
 7     xmlns:jaxws="http://cxf.apache.org/jaxws"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
10                         http://www.springframework.org/schema/context
11                         http://www.springframework.org/schema/context/spring-context-3.1.xsd
12                         http://cxf.apache.org/jaxws
13                         http://cxf.apache.org/schemas/jaxws.xsd
14                         http://cxf.apache.org/bindings/soap
15                         http://cxf.apache.org/schemas/configuration/soap.xsd">
16 
17     
18     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
19         <property name="serviceClass" value="http://www.mamicode.com/cn.bd.client.WeatherService"></property>
20         <property name="address" value="http://localhost:8090/CXFService/getWeather?wsdl"></property>
21     </bean>
22     
23     <bean id="wsClient" class="cn.bd.client.WeatherService" factory-bean="clientFactory" factory-method="create">
24     </bean>
25     
26     <bean id="weatherAction" class="cn.bd.action.WeatherAction" scope="prototype">
27         <property name="weatherService" ref="wsClient"></property>
28     </bean>
29     
30     
31 </beans>

 

5.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>
15   </context-param>
16   <listener>
17      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
18   </listener>
19   
20   <filter>
21       <filter-name>struts2</filter-name>
22       <filter-class>
23           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
24       </filter-class>
25   </filter>
26   <filter-mapping>
27       <filter-name>struts2</filter-name>
28       <url-pattern>/*</url-pattern>
29   </filter-mapping>
30 </web-app>

 

6.action的如下所示例:

 1 package cn.bd.action;
 2 
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.ArrayList;
 6 import java.util.Date;
 7 import java.util.List;
 8 
 9 import cn.bd.client.Weather;
10 import cn.bd.client.WeatherService;
11 import cn.bd.weatherclient.WeatherWebServiceSoap;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 
15 public class WeatherAction extends ActionSupport{
16 
17     /**
18      * 
19      */
20     private static final long serialVersionUID = 1L;
21 
22     private WeatherService weatherService;
23     public void setWeatherService(WeatherService weatherService) {
24         this.weatherService = weatherService;
25     }
26     private List<Weather> list;
27     
28     
29     private WeatherWebServiceSoap serviceSoap;
30     public void setServiceSoap(WeatherWebServiceSoap serviceSoap) {
31         this.serviceSoap = serviceSoap;
32     }
33     
34     
35     
36     @Override
37     public String execute() throws Exception {
38         List<Date> dates=new ArrayList<Date>();
39         DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
40         Date date1=format.parse("2016-12-1");
41         Date date2=format.parse("2016-12-2");
42         Date date3=format.parse("2016-12-3");
43         dates.add(date1);
44         dates.add(date2);
45         dates.add(date3);
46         list=weatherService.getWeathers("北京", dates);//调用web服务
47         
48         /*for(Weather w:list){
49             System.out.println(w.getCity());
50         }*/
51         return super.execute();
52     }
53 
54 
55     
56     
57     public List<Weather> getList() {
58         return list;
59     }
60 
61     public void setList(List<Weather> list) {
62         this.list = list;
63     }
64 }

 

7.struts.xml配置:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
 3     "http://struts.apache.org/dtds/struts-2.1.dtd">
 4 <struts>
 5     <package name="default" namespace="/" extends="struts-default">
 6         <action name="showWeather" class="weatherAction">
 7             <result>index.jsp</result>
 8         </action>
 9     </package>
10 </struts>    

8.在index.jsp页面简单输出:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="s" uri="/struts-tags" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="http://www.mamicode.com/">
12     
13     <title>My JSP ‘index.jsp‘ starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
21     -->
22   </head>
23   
24   <body>
25       <table align="center" style="width:50%;text-align:center;">
26           <tr>
27               <td>城市</td>
28               <td>最高温</td>
29               <td>最低温</td>
30               <td>平均气温</td>
31           </tr>
32           <s:iterator value="http://www.mamicode.com/list" status="sta">
33               <tr <s:if test="#sta.odd==true">style="background-color:gray;"</s:if>>
34                   <td><s:property value="http://www.mamicode.com/city"/></td>
35                   <td><s:property value="http://www.mamicode.com/max"/></td>
36                   <td><s:property value="http://www.mamicode.com/min"/></td>
37                   <td><s:property value="http://www.mamicode.com/average"/></td>
38             </tr>
39         </s:iterator>
40       </table>
41     
42   </body>
43 </html>

 

CXF调用web服务