环境和目录
环境:
jdk1.7.0.79+maven3.5.7+cxf+ssm;
maven项目源码目录:
jdk1.7.0.79+maven3.5.7+cxf+ssm;
maven项目源码目录:
cxf配置:首先自行配置cxf环境变量
1.applicationContext-service.xml
添加以下内容
在头部的beans标签内添加:
在头部的beans标签内添加:
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
需要与原有内容之间加一个空格
然后添加如下节点。
<bean id="weatherServiceImpl" class="com.weather.service.impl.WeatherServiceImpl" />
<jaxws:endpoint id="myService" implementor="#weatherServiceImpl"
address="/ws">
</jaxws:endpoint>
完整的applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 包扫描器,扫描带@Service注解的类 -->
<context:component-scan base-package="com.weather.service"></context:component-scan>
<bean id="weatherServiceImpl" class="com.weather.service.impl.WeatherServiceImpl" />
<jaxws:endpoint id="myService" implementor="#weatherServiceImpl"
address="/ws">
</jaxws:endpoint>
</beans>
2.web.xml
添加以下节点
!-- 配置CXF框架的核心Servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 注解3 -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/weather/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
完整的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="ryzh" version="2.5">
<display-name>weather-manager</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 登录退出session控制 -->
<!--<session-config> <session-timeout>10</session-timeout> </session-config> -->
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>weather-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>weather-manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置CXF框架的核心Servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 注解3 -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/weather/*</url-pattern>
</servlet-mapping>
</web-app>
3.pom.xml
添加以下内容:
<!--web service cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
完整的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ryzh-manager</artifactId>
<groupId>com.ryzh0310</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../ryzh-manager/pom.xml</relativePath>
</parent>
<artifactId>ryzh-manager-web</artifactId>
<packaging>war</packaging>
<!-- 添加依赖 -->
<dependencies>
<dependency>
<groupId>com.ryzh0310</groupId>
<artifactId>ryzh-manager-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<!-- 编辑框kindeditor(使用三个jar:commons-fileupload,json-simple,commons-io) -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<!--web service cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
</dependencies>
</project>
3.webservice注解
发布服务的service还需要添加相关注解。
以WeatherService接口和其实现类为例
WeatherService.java
以WeatherService接口和其实现类为例
WeatherService.java
import javax.jws.WebService;
@WebService
public interface WeatherService {
String listWeather();
}
WeatherServiceImpl.java
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weather.common.JsonUtils;
import com.weather.dao.WeatherMapper;
import com.weather.pojo.WeatherExample;
import com.weather.service.WeatherService;
@Service
@WebService(endpointInterface = "com.weather.service.WeatherService")
public class WeatherServiceImpl implements WeatherService {
@Autowired
WeatherMapper mapper;
@Override
public String listWeather() {
return JsonUtils.objectToJson(mapper
.selectByExample(new WeatherExample()));
}
}
*额外添加一个JsonUtils,放在weather-manager-common项目中
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4.测试运行,检测配置是否正确。
1.maven install parent和common
2.maven build web项目
3.项目发布到tomcat后
根据web.xml和applicationContext-service.xml的配置,将如下url输入浏览器地址栏。
其中的weather是在web.xml中定义的,ws是在applicationContext-service.xml中定义的
如果浏览器返回了结果
http://localhost:8080/weather/ws?wsdl
其中的weather是在web.xml中定义的,ws是在applicationContext-service.xml中定义的
如果浏览器返回了结果
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.weather.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.weather.com/" name="WeatherServiceImplService" targetNamespace="http://impl.service.weather.com/">
<wsdl:import location="http://localhost:8080/weather/ws?wsdl=WeatherService.wsdl" namespace="http://service.weather.com/"></wsdl:import>
<wsdl:binding name="WeatherServiceImplServiceSoapBinding" type="ns1:WeatherService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="listWeather">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="listWeather">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="listWeatherResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherServiceImplService">
<wsdl:port binding="tns:WeatherServiceImplServiceSoapBinding" name="WeatherServiceImplPort">
<soap:address location="http://localhost:8080/weather/ws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
5.生成webservice客户端
首先要安装cxf并配置环境变量。然后在命令行输入wsdl2java -encoding utf-8 -d E:\workspace\weather-manager http://localhost:8080/weather/ws?wsdl
回车
在
E:\workspace\weather-manager
目录下会生成一个文件夹com(就是包名的结构)
将该包拷贝到另一个java项目中,使其作为客户端使用。
新建项目作为客户端访问webservice
WeatherServiceImplService implService = new WeatherServiceImplService();
WeatherService weatherServiceImplPort = implService.getWeatherServiceImplPort();
String listWeather = weatherServiceImplPort.listWeather();
System.err.println(listWeather);
bug:
1.以上内容可以通过webservice访问,通过原本的客户端直接访问失败。
客户端错误:404.
控制台信息:
[org.apache.cxf.transport.servlet.ServletController] Can't find the request for xxx's Observer
错误原因:
在web.xml的注解3这个位置,配置了:
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/weather/*</url-pattern>
</servlet-mapping>
这就是问题所在。
正常的访问也是以weather开头,与webservice访问一样,所以正常的访问也被webservice的servlet给拦截了,所以控制台打印的提示信息,是cxf的类。
只要将该处配置修改成webservice独有的(比如就叫webservice)