1 开发工具
IDEA
2 maven 依赖 pom.xml
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
3 web.xml相关配置
<!-- 配置spring资源路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVCServlet-servlet.xml</param-value>
</context-param>
<!--配置spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 字符集过滤 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 配置cxf的访问地址 -->
<!-- 本系统中webService必须以/ws/*开头 -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
4 spring的相关配置文件 SpringMVCServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:soap="http://cxf.apache.org/bindings/soap">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<context:component-scan base-package="com.wyn.app;com.ws.weather"></context:component-scan>
<!--配置cxf webservice服务-->
<!--配置发布服务端-->
<bean id="weatherInterface" class="com.wyn.app.serviceImp.WeatherInterfaceImpl"></bean>
<!--
发布服务
和使用endpoint发布服务类似
WebService地址=tomcat地址+cxf+/weather
-->
<jaxws:server address="/weather" serviceClass="com.wyn.app.service.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface" />
</jaxws:serviceBean>
</jaxws:server>
<!-- 使用client标签调用服务端 -->
<jaxws:client
id="weatherClient"
address="http://localhost:8080/ws/weather?wsdl"
serviceClass="com.ws.weather.WeatherInterface">
</jaxws:client>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<!-- <bean p:suffix=".jsp" p:prefix="/" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> -->
<!--配置ModelAndView(jsp)解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean>
<!-- 静态资源的访问方式 -->
<mvc:default-servlet-handler/>
</beans>
5 实体类 weatherModel
package com.wyn.app.entity;
import java.util.Date;
/**
* Created by wyn on 2017/8/23.
*/
public class WeatherModel {
//天气概况
private String detail;
//日期
private Date data;
//最高温度
private int temperature_max;
//最低温度
private int temperature_min;
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
public int getTemperature_max() {
return temperature_max;
}
public void setTemperature_max(int temperature_max) {
this.temperature_max = temperature_max;
}
public int getTemperature_min() {
return temperature_min;
}
public void setTemperature_min(int temperature_min) {
this.temperature_min = temperature_min;
}
@Override
public String toString() {
return "WeatherModel{" +
"detail='" + detail + '\'' +
", data=" + data +
", temperature_max=" + temperature_max +
", temperature_min=" + temperature_min +
'}';
}
}
6 service 接口层
package com.wyn.app.service;
import com.wyn.app.entity.WeatherModel;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import java.util.List;
/**
* Created by wangyanan on 2017/8/24.
*/
@WebService(
targetNamespace="http://weather.ws.com/",//指定 wsdl的命名空间
name="WeatherInterface",//指定portType的名称
portName="WeatherInterfacePort",//指定port的名称
serviceName="WeatherService"//服务视图的名称
)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public @WebResult(name="list")
List<WeatherModel> queryWeather(@WebParam(name="cityName")String name);
}
7 serviceImp 接口实现类层
package com.wyn.app.serviceImp;
import com.wyn.app.entity.WeatherModel;
import com.wyn.app.service.WeatherInterface;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by wyn on 2017/8/24.
*/
public class WeatherInterfaceImpl implements WeatherInterface{
public List<WeatherModel> queryWeather(String name) {
//构造测试数据
List<WeatherModel> list = new ArrayList<WeatherModel>();
WeatherModel weatherModel_1 =new WeatherModel();
weatherModel_1.setDetail("晴");
weatherModel_1.setData(new Date());
weatherModel_1.setTemperature_max(30);
weatherModel_1.setTemperature_min(28);
WeatherModel weatherModel_2 =new WeatherModel();
weatherModel_2.setDetail("晴转多云");
weatherModel_2.setData(new Date());
weatherModel_2.setTemperature_max(24);
weatherModel_2.setTemperature_min(20);
list.add(weatherModel_1);
list.add(weatherModel_2);
return list;
}
}
8 接口访问测试