Apachecxf整合SpringMVC
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Apachecxf整合SpringMVC 1.添加依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zouhao</groupId>
<artifactId>bsframe</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.zouhao</groupId>
<artifactId>bsframe-moduel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>bsframe-core</module>
<module>bsframe-entity</module>
<module>bsframe-dao</module>
<module>bsframe-service</module>
<module>bsframe-shiro</module>
<module>bsframe-web</module>
</modules>
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.4</mybatis.version>
<!-- mybatis分页插件 -->
<mybatis-pagehelper.version>4.0.3</mybatis-pagehelper.version>
<!-- log4j⽇志⽂件管理包版本 -->
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<commons-lang3.version>3.3</commons-lang3.version>
<commons-collections.version>3.2.1</commons-collections.version>
<!-- shiro版本号 -->
<shiro.version>1.2.3</shiro.version>
<ehcache.version>2.6.9</ehcache.version>
<servlet-api.version>3.1.0</servlet-api.version>
<cxf.version>2.7.8</cxf.version>
</properties>
<!-- 通⽤jar依赖管理,各个⼦项⽬都默认依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- ⽇志⽂件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections.version}</version>
</dependency>
<dependency>
<groupId>mons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<!-- ⼀般jar依赖管理,各个⼦项⽬按需依赖,不⽤使⽤version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-aspects</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${mybatis-pagehelper.version}</version> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
<!-- apache shior maven start -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcache.version}</version>
</dependency>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- apache shiro maven end -->
<!-- cxf setting starting -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-xmlbeans</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-local</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-jms</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-management</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- CXF REST SETTING -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m10</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.配置CXFServlet。
(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance"
xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>frame</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml;classpath:spring-mybatis.xml;classpath:shiro-config.xml</param-value> </context-param>
<!-- 防⽌发⽣java.beans.Introspector内存泄露,应将它配置在ContextLoaderListener的前⾯ -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Application Context Listener End -->
<!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 -->
<!-- 这⾥filter-name必须对应applicationContext.xml中定义的<bean id="shiroFilter"/> -->
<!-- 使⽤[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->
<!-- 通常会将此filter-mapping放置到最前⾯(即其他filter-mapping前⾯),以保证它是过滤器链中第⼀个起作⽤的 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表⽰⽣命周期由SpringApplicationContext管理,设置为true则表⽰由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC Config Start -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern><!-- 拦截了所有包括静态资源⽂件,spring mvc中需要配置mvc:resources包含静态资源⽂件 --> </servlet-mapping>
<!-- Spring MVC Config End -->
<!-- WebAppRootKey 获取项⽬部署的根路径,将log4j⽇志输出到项⽬的路径下 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<!-- Log4J 配置 Start -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Log4J 配置 End -->
<!-- Spring 编码过滤器 start -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring 编码过滤器 End -->
<!--CXF配置 -->
<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>/cxf/*</url-pattern>
</servlet-mapping>
<!-- 错误跳转页⾯ -->
<!-- <error-page> <error-code>404</error-code> <location>/WEB-INF/errorpage/404.jsp</location>
</error-page> <error-page> <error-code>405</error-code> <location>/WEB-INF/errorpage/405.jsp</location>
</error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/errorpage/500.jsp</location>
</error-page> -->
</web-app>
3.创建SOAP服务接⼝
package org.bsframe.web.cxf;
import javax.jws.WebMethod;
@WebService
public interface HelloWorldService {
@WebMethod
public String sayHello(@WebParam(name="username") String username);
}
4.SOAP服务实现
package org.bsframe.web.cxf;
import javax.jws.WebParam;
import javax.jws.WebService;
import er;
import erServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
@WebService(endpointInterface="org.bsframe.web.cxf.HelloWorldService",serviceName="helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService
{
@Autowired
private UserServiceImpl userServiceImpl;
public String sayHello(@WebParam(name="userName") String userName) {
User user = userServiceImpl.findUserById(2);
System.out.println(user);
System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");
return "Hello "+userName;
}
}
5.Restfull服务实现
package org.bsframe.web.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import er;
import ponent;
@Component("restSample")
public class CxfController {
@GET
@Path("/order")
@Produces({ MediaType.APPLICATION_JSON })
public User findUser() {
User user = new User();
user.setUsername("zouhao");
user.setPassword("zouhao");
return user;
}
}
5.添加spring配置⽂件applicationContext.xml,注册jaxws:endpoint,以及jaxrs:server <beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:jaxws="/jaxws"
xmlns:jaxrs="/jaxrs"
xmlns:context="/schema/context"
xmlns:jpa="/schema/data/jpa"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-4.0.xsd
/schema/tx
/schema/tx/spring-tx-4.0.xsd
/schema/context
/schema/context/spring-context-4.0.xsd
/schema/data/jpa
/schema/data/jpa/spring-jpa.xsd
/schema/aop
/schema/aop/spring-aop-4.0.xsd
/schema/data/jpa
/schema/data/jpa/spring-jpa.xsd
/jaxws
/schemas/jaxws.xsd
/jaxrs
/schemas/jaxrs.xsd">
<!-- 引⼊jdbc配置⽂件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 扫描⽂件(⾃动将servicec层注⼊) -->
<context:component-scan base-package="org.bsframe"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<bean id="helloWorldImpl" class="org.bsframe.web.cxf.HelloWorldServiceImpl"></bean>
<!-- soap webservice -->
<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl" address="/HelloWorld" />
<!-- <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" />
<jaxws:endpoint id="helloWorld" implementor="org.bsframe.web.cxf.HelloWorldServiceImpl"
address="/HelloWorld" /> -->
<!-- restful webservice -->
<jaxrs:server id="rs1" address="/rs">
<jaxrs:serviceBeans>
<ref bean="restSample" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>
<wsdl:definitions name="helloWorldService" targetNamespace="/"><wsdl:types><xs:schema elementFormDefault="unqualified" targetNamespace="/" version="1.0"><xs:element
</wsdl:part></wsdl:message><wsdl:message name="sayHello"><wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part></wsdl:message><wsdl:portType name="HelloWorldService"><wsdl:operation name="sayHello"><wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input><wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService"><soap:binding style="document" transport="/soap/http"/><wsdl:operation 7.通过Main()⽅法进⾏测试SOAP
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.bsframe.web.cxf.HelloWorldService;
public class ServiceTest {
public static void main(String[] args) throws Exception {
JaxWsProxyFactoryBean webService = new JaxWsProxyFactoryBean();
webService.setServiceClass(HelloWorldService.class);
webService.setAddress("http://localhost:8080/bsframe-web/cxf/HelloWorld");
HelloWorldService userService = (HelloWorldService) webService.create();
System.out.println(userService.sayHello("testtttttttttt"));
//System.out.println(userService.checkUser("abc", "123"));
}
}
{"id":null,"username":"zouhao","password":"zouhao","nickname":null,"parentid":null,"enable":null,"reserved1":null,"created":null,"modified":null,"roles":null}。