spring + springmvc + cxf 实现webservice restful

web.xml 配置

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <display-name>workflow</display-name>  
  7.     <context-param>  
  8.         <param-name>webAppRootKey</param-name>  
  9.         <param-value>jeecg</param-value>  
  10.     </context-param>  
  11.     <context-param>  
  12.         <param-name>log4jConfigLocation</param-name>  
  13.         <param-value>classpath:log4j.properties</param-value>  
  14.     </context-param>  
  15.     <listener>  
  16.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  17.     </listener>  
  18.     <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->  
  19.     <context-param>  
  20.         <param-name>contextConfigLocation</param-name>  
  21.         <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/cxf-server.xml</param-value>  
  22.     </context-param>  
  23.     <listener>  
  24.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  25.     </listener>  
  26.     <!-- 防止内存溢出监听器 -->  
  27.     <listener>  
  28.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  29.     </listener>  
  30.     <filter>  
  31.         <filter-name>openSessionInViewFilter</filter-name>  
  32.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  33.         <init-param>  
  34.             <param-name>singleSession</param-name>  
  35.             <param-value>true</param-value>  
  36.         </init-param>  
  37.     </filter>  
  38.     <filter-mapping>  
  39.         <filter-name>openSessionInViewFilter</filter-name>  
  40.         <url-pattern>*.do</url-pattern>  
  41.     </filter-mapping>  
  42.       
  43.     <listener>  
  44.         <description>Introspector缓存清除监听器</description>  
  45.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  46.     </listener>  
  47.     <listener>  
  48.         <description>request监听器</description>  
  49.         <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>  
  50.     </listener>  
  51.       
  52.     <!-- 配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 -->  
  53.     <servlet>  
  54.         <servlet-name>workflow</servlet-name>  
  55.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  56.         <load-on-startup>1</load-on-startup>  
  57.     </servlet>  
  58.     <servlet-mapping>  
  59.         <servlet-name>workflow</servlet-name>  
  60.         <!-- 配置所有的页面 -->  
  61.         <url-pattern>*.do</url-pattern>  
  62.     </servlet-mapping>  
  63.   
  64.     <!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->  
  65.     <filter>  
  66.         <filter-name>Spring character encoding filter</filter-name>  
  67.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  68.         <init-param>  
  69.             <param-name>encoding</param-name>  
  70.             <param-value>utf-8</param-value>  
  71.         </init-param>  
  72.     </filter>  
  73.     <filter-mapping>  
  74.         <filter-name>Spring character encoding filter</filter-name>  
  75.         <url-pattern>/*</url-pattern>  
  76.     </filter-mapping>  
  77.     <!-- 配置cxfservlet -->  
  78.     <servlet>  
  79.         <servlet-name>CXFService</servlet-name>  
  80.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  81.     </servlet>  
  82.     <servlet-mapping>  
  83.         <servlet-name>CXFService</servlet-name>  
  84.         <url-pattern>/ws/*</url-pattern>  
  85.     </servlet-mapping>  
  86.   
  87.     <session-config>  
  88.         <session-timeout>30</session-timeout>  
  89.     </session-config>  
  90.     <welcome-file-list>  
  91.         <welcome-file>index.jsp</welcome-file>  
  92.     </welcome-file-list>  
  93. </web-app>  

 applicationContext.xml spring的配置

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="   
  6.           http://www.springframework.org/schema/beans   
  7.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.           http://www.springframework.org/schema/tx   
  9.           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.           http://www.springframework.org/schema/context   
  11.           http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  12.           http://www.springframework.org/schema/aop   
  13.           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  14.       
  15.     <!-- 引用文件 -->  
  16.     <context:property-placeholder location="WEB-INF/classes/jdbc.properties" />  
  17.       
  18.     <context:component-scan base-package="com.workflow.dao,com.workflow.service,com.workflow.api.service,com.workflow.business.service,com.weir.webservice" />  
  19.   
  20.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  21.         destroy-method="close">  
  22.         <property name="driverClass">  
  23.             <value>${c3p0.driverClass}</value>  
  24.         </property>  
  25.         <property name="jdbcUrl">  
  26.             <value>${c3p0.jdbcUrl}</value>  
  27.         </property>  
  28.         <property name="user">  
  29.             <value>${c3p0.user}</value>  
  30.         </property>  
  31.         <property name="password">  
  32.             <value>${c3p0.password}</value>  
  33.         </property>  
  34.   
  35.         <!--连接池中保留的最小连接数。 -->  
  36.         <property name="minPoolSize">  
  37.             <value>${c3p0.minPoolSize}</value>  
  38.         </property>  
  39.   
  40.         <!--连接池中保留的最大连接数。Default: 15 -->  
  41.         <property name="maxPoolSize">  
  42.             <value>${c3p0.maxPoolSize}</value>  
  43.         </property>  
  44.   
  45.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  46.         <property name="initialPoolSize">  
  47.             <value>${c3p0.initialPoolSize}</value>  
  48.         </property>  
  49.   
  50.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  51.         <property name="maxIdleTime">  
  52.             <value>${c3p0.maxIdleTime}</value>  
  53.         </property>  
  54.   
  55.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
  56.         <property name="acquireIncrement">  
  57.             <value>${c3p0.acquireIncrement}</value>  
  58.         </property>  
  59.   
  60.         <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。   
  61.             如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->  
  62.         <property name="maxStatements">  
  63.             <value>${c3p0.maxStatements}</value>  
  64.         </property>  
  65.   
  66.   
  67.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
  68.         <property name="idleConnectionTestPeriod">  
  69.             <value>${c3p0.idleConnectionTestPeriod}</value>  
  70.         </property>  
  71.   
  72.         <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->  
  73.         <property name="acquireRetryAttempts">  
  74.             <value>${c3p0.acquireRetryAttempts}</value>  
  75.         </property>  
  76.   
  77.         <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试   
  78.             获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->  
  79.         <property name="breakAfterAcquireFailure">  
  80.             <value>${c3p0.breakAfterAcquireFailure}</value>  
  81.         </property>  
  82.   
  83.         <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable   
  84.             等方法来提升连接测试的性能。Default: false -->  
  85.         <property name="testConnectionOnCheckout">  
  86.             <value>${c3p0.testConnectionOnCheckout}</value>  
  87.         </property>  
  88.     </bean>  
  89.   
  90.     <bean id="sessionFactory"  
  91.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  92.         <property name="dataSource">  
  93.             <ref bean="dataSource" />  
  94.         </property>  
  95.         <property name="packagesToScan">  
  96.             <list>  
  97.                 <value>com.workflow.entity</value>  
  98.                 <value>com.workflow.api.entity</value>  
  99.                 <value>com.workflow.business.entity</value>  
  100.             </list>  
  101.         </property>  
  102.         <property name="hibernateProperties">  
  103.             <props>  
  104.                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
  105.                 <prop key="hibernate.show_sql">true</prop>  
  106.                 <prop key="hibernate.query.substitutions">  
  107.                     true 'T', false 'F'  
  108.                 </prop>  
  109.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  110.             </props>  
  111.         </property>  
  112.     </bean>  
  113.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  114.         <property name="sessionFactory" ref="sessionFactory" />  
  115.     </bean>  
  116.     <!-- 注解方式配置事物 -->  
  117.     <tx:annotation-driven transaction-manager="transactionManager" />  
  118. </beans>  

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

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                        http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
  9.                        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">  
  10.   
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  14.   
  15.     <!-- <jaxws:endpoint id="userExit" implementor="com.weir.webservice.UserExitImpl" address="/userExit" /> -->  
  16.       
  17.     <!-- http://172.168.1.172:8080/workflow/ws -->  
  18.     <jaxrs:server id="userExit" serviceClass="com.weir.webservice.UserExitImpl" address="/api"></jaxrs:server>  
  19. </beans>  

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

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xmlns:context="http://www.springframework.org/schema/context"  
  5.  xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.       
  10.     <context:component-scan base-package="com.workflow.controller,com.workflow.api,com.workflow.business" />  
  11.     <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->  
  12.     <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
  13.         <property name="supportedMediaTypes">  
  14.             <list>  
  15.                 <value>application/json</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.   
  20.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  21.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  22.         <property name="messageConverters">  
  23.             <list>  
  24.                 <ref bean="fastJsonHttpMessageConverter" />  
  25.             </list>  
  26.         </property>  
  27.     </bean>  
  28.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
  29.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  30.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
  31.         <property name="prefix" value="/"></property>  
  32.         <property name="suffix" value=".jsp"></property>  
  33.     </bean>  
  34.   
  35. </beans>  

 

所需要的jar包  全部截图:



 

 例子很简单  没有涉及业务

接口:

Java代码   收藏代码
  1. package com.weir.webservice;  
  2.   
  3. import javax.jws.WebService;  
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.Path;  
  6.   
  7. //@WebService  
  8. @Path(value="/userexit")  
  9. public interface UserExit {  
  10.   
  11.     @GET  
  12.     @Path("/exitUser")  
  13.     public boolean exitUser();  
  14. }  

 

实现:

Java代码   收藏代码
  1. package com.weir.webservice;  
  2.   
  3. import javax.jws.WebService;  
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.Path;  
  6.   
  7. //@WebService(endpointInterface="com.weir.webservice.UserExit")  
  8. @Path(value="/userexit")  
  9. public class UserExitImpl implements UserExit {  
  10.   
  11.     @GET  
  12.     @Path("/exitUser")  
  13.     public boolean exitUser() {  
  14.         System.out.println("lllll");  
  15.         return false;  
  16.     }  
  17.   
  18. }  

 

启动tomcat就可以了:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值