commons-lang
commons-lang
2.6
org.codehaus.jackson
jackson-jaxrs
1.9.2
javax.ws.rs
javax.ws.rs-api
2.0.1
org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}
org.apache.cxf
cxf-rt-transports-http
${cxf.version}
org.apache.cxf
cxf-rt-frontend-jaxrs
${cxf.version}
org.apache.cxf
cxf-rt-ws-security
${cxf.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aspects
${spring.version}
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.1
mysql
mysql-connector-java
5.1.38
c3p0
c3p0
0.9.1.2
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
junit
junit
4.12
test
另外,建议在maven中加上bundle插件,可能有些依赖是以bundle形式发布,如下
org.apache.felix
maven-bundle-plugin
3.3.0
数据库sql文件会放在打包的项目文件当中一起上传
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:beans-mvc.xml
1
DispatcherServlet
/
另外,beans-mvc.xml配置文件如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.study.springcxf.action" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
bean>
beans>
contextConfigLocation
classpath:beans-core.xml
org.springframework.web.context.ContextLoaderListener
beans-core.xml文件配置如下,其中
即引入的Apache CXF配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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">
<import resource="classpath:beans-cxf.xml"/>
<context:component-scan base-package="com.study.springcxf">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root" />
<property name="password" value="0000"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_cxf" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="10" />
bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/study/springcxf/mapper/*Mapper.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.study.springcxf.mapper" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="new*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="change*" propagation="REQUIRED"/>
<tx:method name="check*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.study.springcxf.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
aop:config>
beans>
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/webservices/*
beans-cxf.xml配置文件如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:server address="/calculateService" serviceClass="com.study.springcxf.service.CalculateService" serviceBean="#calculateService">
<jaxws:inInterceptors>
<ref bean="loggingInInterceptor"/>
<ref bean="userCheckInterceptor"/>
jaxws:inInterceptors>
jaxws:server>
<jaxws:endpoint address="/userService" implementor="com.study.springcxf.service.UserServiceImpl">
<jaxws:inInterceptors>
<ref bean="userCheckInterceptor"/>
jaxws:inInterceptors>
jaxws:endpoint>
<jaxrs:server address="/restful">
<jaxrs:serviceBeans>
<ref bean="customerService"/>
jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">bean>
jaxrs:providers>
jaxrs:server>
beans>
@Override
public void handleMessage(SoapMessage message) throws Fault {
Header header = message.getHeader(new QName("user"));
if (header!=null) {
Element userElement = (Element) header.getObject();
String username = userElement.getElementsByTagName("username").item(0).getTextContent();
String password = userElement.getElementsByTagName("password").item(0).getTextContent();
// 实际发送的请求 uri
String uri = message.getDestination().getAddress().getAddress().getValue();
try {
boolean permit = permissionService.checkUserPermission(username, password, uri);
if (permit) {
logger.log(Level.INFO, "验证权限通过...");
return ;
}
} catch (Fault e) {
throw e;
}
} else {
throw new Fault("请输入用户名密码!", logger);
}
}
@Override
public boolean checkUserPermission(String username, String password, String accessedResource) {
List resourceUris = new ArrayList();
User user = userService.findUserByUsernameAndPassword(username, password);
if (user==null) {
throw new Fault("用户名密码不正确!", logger);
}
List userResources = userResourceService.findResourceByUserId(user.getId());
if (userResources==null || userResources.size()<1) {
throw new Fault("该用户没有访问任何资源的权限!", logger);
}
List resourceIds = new ArrayList();
for (UserResource e : userResources) {
resourceIds.add(e.getResourceId());
}
List resources = resourceService.findResourceIn(resourceIds);
if (resources==null || resources.size()<1) {
throw new Fault("对应资源已被禁用或者移除!", logger);
}
for (Resource e : resources) {
resourceUris.add(e.getResourceUri());
}
if (resourceUris.size()>0 && resourceUris.contains(accessedResource)) {
return true;
} else {
throw new Fault("该用户没有权限访问对应资源,该用户能访问的资源路径如下:" + resourceUris, logger);
}
}
<jaxws:server address="/calculateService" serviceClass="com.study.springcxf.service.CalculateService" serviceBean="#calculateService">
<jaxws:inInterceptors>
<ref bean="loggingInInterceptor"/>
<ref bean="userCheckInterceptor"/>
jaxws:inInterceptors>
jaxws:server>
表示通过接口CalculateService在/calculateService提供服务,实现类的实例为calculateService,注意前面要加上#,或者用上面配置的方式二。两个ref元素表示日志拦截器和用户名密码检查拦截器,配置在jaxws:inInterceptors元素中。另外如果需要对异常问题进行处理,那么让拦截器类重写handleFault方法,并在配置文件中加上jaxws:inFaultInterceptors元素即可。最后,本实例还实现了一个restful webservice,在beans-cxf.xml文件的最后address为/restful部分
发布程序到tomcat,通过http://127.0.0.1:8081/spring-cxf/webservices/在浏览器中可以查看已经发布的服务,截图如下
发布的服务分两大部分显示,上面的显示为soap webservice,下面的是restful webservice。请求路径中的/webservices是web.xml配置的CXFServlet的url-pattern
客户端可以通过http://127.0.0.1:8081/spring-cxf/webservices/calculateService?wsdl请求路径来生成客户端代码,使用apache cxf工具生成命令如下:wsdl2java -encoding utf-8 http://127.0.0.1:8081/spring-cxf/webservices/calculateService?wsdl。wsdl2java命令使用可以参考官方文档