`
weir2009
  • 浏览: 263813 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

spring + springmvc + cxf 实现webservice restful

 
阅读更多



 做了一个小例子。

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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>workflow</display-name>
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>jeecg</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/cxf-server.xml</param-value>
    </context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	
	<listener>
		<description>Introspector缓存清除监听器</description>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<listener>
		<description>request监听器</description>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	
	<!-- 配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 -->
	<servlet>
		<servlet-name>workflow</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>workflow</servlet-name>
		<!-- 配置所有的页面 -->
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->
	<filter>
		<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
    <!-- 配置cxfservlet -->
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>

    <session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 applicationContext.xml spring的配置

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 引用文件 -->
	<context:property-placeholder location="WEB-INF/classes/jdbc.properties" />
	
	<context:component-scan base-package="com.workflow.dao,com.workflow.service,com.workflow.api.service,com.workflow.business.service,com.weir.webservice" />

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass">
			<value>${c3p0.driverClass}</value>
		</property>
		<property name="jdbcUrl">
			<value>${c3p0.jdbcUrl}</value>
		</property>
		<property name="user">
			<value>${c3p0.user}</value>
		</property>
		<property name="password">
			<value>${c3p0.password}</value>
		</property>

		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize">
			<value>${c3p0.minPoolSize}</value>
		</property>

		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize">
			<value>${c3p0.maxPoolSize}</value>
		</property>

		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize">
			<value>${c3p0.initialPoolSize}</value>
		</property>

		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime">
			<value>${c3p0.maxIdleTime}</value>
		</property>

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement">
			<value>${c3p0.acquireIncrement}</value>
		</property>

		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
			如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements">
			<value>${c3p0.maxStatements}</value>
		</property>


		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod">
			<value>${c3p0.idleConnectionTestPeriod}</value>
		</property>

		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts">
			<value>${c3p0.acquireRetryAttempts}</value>
		</property>

		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
			获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
		<property name="breakAfterAcquireFailure">
			<value>${c3p0.breakAfterAcquireFailure}</value>
		</property>

		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
			等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout">
			<value>${c3p0.testConnectionOnCheckout}</value>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.workflow.entity</value>
				<value>com.workflow.api.entity</value>
				<value>com.workflow.business.entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.query.substitutions">
					true 'T', false 'F'
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 注解方式配置事物 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 cxf-server.xml  cxf 服务器端的配置  这里我没有和spring的配置放在一起,而是独立出来的

<?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"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!-- <jaxws:endpoint id="userExit" implementor="com.weir.webservice.UserExitImpl" address="/userExit" /> -->
    
    <!-- http://172.168.1.172:8080/workflow/ws -->
    <jaxrs:server id="userExit" serviceClass="com.weir.webservice.UserExitImpl" address="/api"></jaxrs:server>
</beans>

 workflow-servlet.xml sprinmvc 配置文件也贴出来吧

<?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: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-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	
	<context:component-scan base-package="com.workflow.controller,com.workflow.api,com.workflow.business" />
	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json</value>
			</list>
		</property>
	</bean>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="fastJsonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<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>

</beans>

 

所需要的jar包  全部截图:



 

 例子很简单  没有涉及业务

接口:

package com.weir.webservice;

import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

//@WebService
@Path(value="/userexit")
public interface UserExit {

	@GET
	@Path("/exitUser")
	public boolean exitUser();
}

 

实现:

package com.weir.webservice;

import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

//@WebService(endpointInterface="com.weir.webservice.UserExit")
@Path(value="/userexit")
public class UserExitImpl implements UserExit {

	@GET
	@Path("/exitUser")
	public boolean exitUser() {
		System.out.println("lllll");
		return false;
	}

}

 

启动tomcat就可以了:



 

  • 大小: 77.1 KB
  • 大小: 58.8 KB
  • 大小: 28.7 KB
3
3
分享到:
评论
2 楼 在世界的中心呼喚愛 2014-12-11  
解决了。很是奇怪,spring 配置文件放在src下,就有那种问题。。如果放在web-inf下则ok
1 楼 在世界的中心呼喚愛 2014-12-11  
这个cxf的restfull风格和spring mvc 结合起来会冲突?
我如果支持spring mvc的restfull风格,调用不了cxf的。。
反过来也是一样

相关推荐

Global site tag (gtag.js) - Google Analytics