spring spring-mvc cxf maven结合
1、eclipse创建maven项目
2、配置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>
<groupId>com.it.cxf</groupId>
<artifactId>springmvccxf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.3.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <!--spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency> <!--web service 以下都是cxf必备的 --> <!--org.apache.cxf.transport.servlet.CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency> <!--不加这个包会报错Unable to locate spring NamespaceHandler for XML schema namespace
[http://cxf.apache.org/jaxws] -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency> <!--java实现webservice,不部署到tomcat,需要jetty包支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.8</version>
</dependency>
<!-- WEB -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!-- 配置tomcat7 -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/cxf</path>
<uriEncoding>UTF-8</uriEncoding>
<finalName>cxf</finalName>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、创建MyWebService接口
/**
* MyWebService.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package com.it.springmvccxf;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface MyWebService {
public int add(@WebParam(name = "firstA") int a, @WebParam(name = "firstB") int b);
public int minus(@WebParam(name = "secondA") int a, @WebParam(name = "secondB") int b);
}
4、创建MyWebServiceImpl实现类
package com.it.springmvccxf.impl;
import javax.jws.WebService;
import com.it.springmvccxf.MyWebService;
@WebService(endpointInterface="com.it.springmvccxf.MyWebService")
public class MyWebServiceImpl implements MyWebService {
@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a + b));
return a + b;
}
@Override
public int minus(int a, int b) {
System.out.println(a + "-" + b + "=" + (a - b));
return a - b;
}
}
5、配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--配置springmvc --> <!--1.开启springmvc注解模式 -->
<mvc:annotation-driven /> <!--servlet-mapping -->
<!--2静态资源默认的servlet配置,(1)允许对静态资源的处理:js,gif (2)允许使用“/”做整体映射 -->
<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-servlet-handler />
<!--3:配置jsp 显示viewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 4自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.it.springmvccxf" />
<!-- 定义无需Controller的url<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:/index" />
</beans>
6、配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="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"
default-lazy-init="true">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<!--<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> -->
<!--发布webservice -->
<!-- WebService的实现Bean定义 -->
<!--web.xml配置了webservice的访问路径/server/*,那么/server/web-publish?wsdl就是该webservice的访问路径 -->
<bean id="webserviceServer" class="com.it.springmvccxf.impl.MyWebServiceImpl" />
<!-- jax-ws endpoint定义 -->
<jaxws:endpoint id="myService" implementor="#webserviceServer" address="/web-publish">
</jaxws:endpoint>
<!--发布webservice -->
</beans>
7、配置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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 处理中文编码 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--Spring MVC是通过DispatcherServlet来加载Spring配置文件的,因此不需要在web.xml中配置ContextLoaderListener。
但是CXF却需要通过ContextLoaderListener来加载Spring。 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置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>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/server/*</url-pattern>
</servlet-mapping>
</web-app>
8、启动项目
这里一定要注意spring-mvc与cxf使用不同的容器加载,否则会报错:
springmvc是不需要ContextLoaderListener来加载管理bean的,DispatcherServlet它有自己的容器,主要用于加载除控制器层的bean,DispatcherServlet属于子容器。
cxf需要ContextLoaderListener,cxf需要ContextLoaderListener是spring管理bean的父容器,一般用于加载非控制器层的bean。
子容器可以访问父容器中的bean,而父容器不能访问子容器。
可以把基本web请求的controller等这些bean放到spring-web.xml中,让DispatcherServlet去加载管理spring-web.xml。
把webservice相关配置到另外一个xml文件中,比如spring-webservice.xml,
让ContextLoaderListener去加载管理spring-webservice.xml和其他spring文件()spring-mysql.xml,spring-jpa.xml等),
这样,就互不影响了。
而且:ContextLoaderListene默认会加载applicationContext.xml这个名字的文件,如果定义为spring-context.xml会报错,重命名需要context-param
此时配置tomcat启动项目后便暴露了wsd
在地址栏中输入http://localhost:8080/cxf/server/web-publish?wsdl,可以查看生成的wsdl文件如下:
<wsdl:definitions name="MyWebServiceImplService" targetNamespace="http://impl.springmvccxf.it.com/">
<wsdl:import
location="http://localhost:8080/cxf/server/web-publish?wsdl=MyWebService.wsdl"
namespace="http://springmvccxf.it.com/">
</wsdl:import>
<wsdl:binding name="MyWebServiceImplServiceSoapBinding"
type="ns1:MyWebService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="add">
<soap:operation soapAction="" style="document" />
<wsdl:input name="add">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="minus">
<soap:operation soapAction="" style="document" />
<wsdl:input name="minus">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="minusResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyWebServiceImplService">
<wsdl:port binding="tns:MyWebServiceImplServiceSoapBinding"
name="MyWebServiceImplPort">
<soap:address location="http://localhost:8080/cxf/server/web-publish" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
9、调用
package com.it.springmvccxf.util;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.it.springmvccxf.MyWebService;
public class ClientForCXF {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyWebService.class);
factoryBean.setAddress("http://localhost:8080/cxf/server/web-publish");
MyWebService myWebService=(MyWebService)factoryBean.create();
int result;
result = myWebService.add(1, 1);
System.out.println(result);
}
}
10、wsdl2java生成独立客户端代码并调用
以上两种实现webservice的方法仅适用于自己有java的webservice的服务端,自己调用。而往往服务端可能由别的语言实现,或者服务端并非我们自己实现,我们没有服务端接口,我们只能获得暴漏的wsdl,并进行调用,这就需要使用wsdl2java生成该wsdl的java客户端并调用了
输入
作者: 时间丶思考
链接:http://www.imooc.com/article/14635
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
输入
作者: 时间丶思考
链接:http://www.imooc.com/article/14635
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
输入
wsdl2java -encoding utf-8 -d D:\ws\wsdl2javatest http://localhost:8080/cxf/server/web-publish?wsdl
生成文件结构如下:
这时,我们变可以写客户端并调用了
package com.it.springmvccxf.impl;
import com.it.springmvccxf.MyWebService;
public class Test {
public static void main(String[] args) {
MyWebServiceImplService implService=new MyWebServiceImplService();
MyWebService service=implService.getMyWebServiceImplPort();
System.out.println(service.add(1, 1));
}
}