获取 star ngs efault element mls file default
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 * <complexType name="weather">
- 16 * <complexContent>
- 17 * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- 18 * <sequence>
- 19 * <element name="average" type="{http://www.w3.org/2001/XMLSchema}int"/>
- 20 * <element name="city" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
- 21 * <element name="date" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/>
- 22 * <element name="desc" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
- 23 * <element name="max" type="{http://www.w3.org/2001/XMLSchema}int"/>
- 24 * <element name="min" type="{http://www.w3.org/2001/XMLSchema}int"/>
- 25 * </sequence>
- 26 * </restriction>
- 27 * </complexContent>
- 28 * </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 publicjava.util.List getWeathers(
- 27@WebParam(name = "city", targetNamespace = "")
- 28 java.lang.String city,
- 29@WebParam(name = "dates", targetNamespace = "")
- 30java.util.List dates
- 31 );
- 32}
4.spring 容器的配置如下:
- 1 2<beans
- 3xmlns="http://www.springframework.org/schema/beans" 4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5xmlns:p="http://www.springframework.org/schema/p" 6xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jaxws="http://cxf.apache.org/jaxws"
- 8xsi:schemaLocation="http://www.springframework.org/schema/beans 9http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- 10http://www.springframework.org/schema/context
- 11http://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
- 18class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">19serviceClass" value="cn.bd.client.WeatherService">20address" value="http://localhost:8090/CXFService/getWeather?wsdl">2122
- 23class="cn.bd.client.WeatherService"factory-bean="clientFactory" factory-method="create">2425
- 26class="cn.bd.action.WeatherAction" scope="prototype">27
- 2829
- 30
- 31
5.web.xml 示例如下:
- 1 2 3xmlns="http://java.sun.com/xml/ns/javaee" 4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- 7 8
- 9index.jsp1011
- 12
- 13contextConfigLocation14classpath:applicationContext.xml1516
- 17class>org.springframework.web.context.ContextLoaderListenerclass>1819
- 20
- 21struts222class>23 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- 24class>2526
- 27struts228 /*</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 classWeatherActionextends ActionSupport{
- 16
- 17 /**
- 18 *
- 19 */
- 20 private static final longserialVersionUID = 1L;
- 21
- 22 private WeatherService weatherService;
- 23 public void setWeatherService(WeatherService weatherService) {
- 24 this.weatherService = weatherService;
- 25 }
- 26 privateList list;
- 27
- 28
- 29 private WeatherWebServiceSoap serviceSoap;
- 30 public void setServiceSoap(WeatherWebServiceSoap serviceSoap) {
- 31 this.serviceSoap = serviceSoap;
- 32 }
- 33
- 34
- 35
- 36 @Override
- 37 publicString execute()throws Exception {
- 38List dates=newArrayList();
- 39DateFormat format=newSimpleDateFormat("yyyy-MM-dd");
- 40Date date1=format.parse("2016-12-1");
- 41Date date2=format.parse("2016-12-2");
- 42Date date3=format.parse("2016-12-3");
- 43 dates.add(date1);
- 44 dates.add(date2);
- 45 dates.add(date3);
- 46list=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 publicList getList() {
- 58 return list;
- 59 }
- 60
- 61 public voidsetList(List list) {
- 62 this.list = list;
- 63 }
- 64}
7.struts.xml 配置:
- 1 2 3"http://struts.apache.org/dtds/struts-2.1.dtd"> 4
- 5<packagename="default" namespace="/"extends="struts-default"> 6class="weatherAction"> 7index.jsp 8 9</package>10
8. 在 index.jsp 页面简单输出:
- 1<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%> 2<%@ taglib prefix="s" uri="/struts-tags" %> 3<% 4String path = request.getContextPath();
- 5String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- 6%> 7
- 8 9101112
- 1314151617181920
- 21-->2223
- 2425
- 2627城市28最高温29最低温30平均气温3132
- 33iftest="#sta.odd==true">style="background-color:gray;"if>>34
- 35
- 36
- 37
- 38394041
- 4243
CXF 调用 web 服务
来源: http://www.bubuko.com/infodetail-2019158.html