1.RESTful API接口定义
/* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this file except in compliance with the License. * A copy of the License is located at * http://www.witpool.org/licenses * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package org.witpool.rest; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import org.witpool.common.model.bean.WitEntity; import org.witpool.common.model.po.WitAccount; /** * @ClassName: IWitAccount * @Description: Account service definition * @author Dom Wang * @date 2017-11-11 AM 11:21:55 * @version 1.0 */ @Path("/account") public interface IWitAccount { /** * * * @Title: addAccount * @Description: Add account * @param @param account * @param @return * @return WitEntity<WitAccount> * @throws */ @POST WitEntity<WitAccount> addAccount(WitAccount account); /** * * * @Title: updateAccount * @Description: Update account * @param @param account * @param @return * @return WitEntity<WitAccount> * @throws */ @PUT WitEntity<WitAccount> updateAccount(WitAccount account); /** * * * @Title: delAccount * @Description: Delete account by user ID * @param @param userId * @param @return * @return WitEntity<WitAccount> * @throws */ @DELETE @Path("/{userId}") WitEntity<WitAccount> delAccount(@PathParam("userId") Integer userId); /** * * * @Title: getAccount * @Description: Query account by user ID * @param @param userId * @param @return * @return WitEntity<WitAccount> * @throws */ @GET @Path("/{userId}") WitEntity<WitAccount> getAccount(@PathParam("userId") Integer userId); /** * * * @Title: getAccount * @Description: Query all the accounts * @param @param userId * @param @return * @return WitEntity<WitAccount> * @throws */ @GET @Path("/all") WitEntity<WitAccount> getAccounts(); }
2.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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"/> <import resource="classpath:persist-config.xml"/> <jaxrs:server id="witpool" address="/"> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </jaxrs:outInterceptors> <jaxrs:providers> <ref bean="jacksonProvider"/> </jaxrs:providers> <jaxrs:extensionMappings> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </jaxrs:extensionMappings> <jaxrs:serviceBeans> <ref bean="witAccount"/> </jaxrs:serviceBeans> </jaxrs:server> <bean id="jaxbAnnotationIntrospector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector"/> <bean id="jsonmapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:annotationIntrospector-ref="jaxbAnnotationIntrospector"> <property name="featuresToEnable"> <array> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT"/> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/> </array> </property> <property name="featuresToDisable"> <array> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_NULL_MAP_VALUES"/> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_EMPTY_JSON_ARRAYS"/> </array> </property> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"></bean> </property> </bean> <bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"> <property name="mapper" ref="jsonmapper"/> </bean> <bean id="witAccount" class="org.witpool.rest.impl.WitAccountImpl"/> </beans>
3.Spring与Hibernate的集成配置
<?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.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!-- Scan Bean --> <context:component-scan base-package="org.witpool.common.model.po"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:resources.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias" value="proxoolDataSource"/> <property name="driver" value="${connection.driver_class}"/> <property name="driverUrl" value="${connection.url}"/> <property name="user" value="${connection.username}"/> <property name="password" value="${connection.password}"/> <property name="maximumConnectionCount" value="${proxool.maximum.connection.count}"/> <property name="minimumConnectionCount" value="${proxool.minimum.connection.count}"/> <property name="statistics" value="${proxool.statistics}"/> <property name="simultaneousBuildThrottle" value="${proxool.simultaneous.build.throttle}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan"> <list> <value>org.witpool.common.model.po</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop> <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop> <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop> <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.bytecode.use_reflection_optimizer}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <aop:config> <aop:pointcut id="rest-api" expression="execution(* org.witpool.rest.*.*(..))"/> <aop:advisor pointcut-ref="rest-api" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="query*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="save" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <bean id="baseDao" class="org.witpool.persist.dao.impl.BaseDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> <property name="baseDao" ref="baseDao"/> </bean> </beans>
4.Hibernate的参数配置
hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.query.substitutions=true 1, false 0 hibernate.default_batch_fetch_size=16 hibernate.max_fetch_depth=2 hibernate.bytecode.use_reflection_optimizer=true hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory net.sf.ehcache.configurationResourceName=/ehcache_hibernate.xml hibernate.cache.use_structured_entries=true hibernate.generate_statistics=true connection.driver_class=com.mysql.jdbc.Driver connection.url=jdbc:mysql://localhost:3306/witpool?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true connection.username=root connection.password=123456 proxool.maximum.connection.count=40 proxool.minimum.connection.count=5 proxool.statistics=1m,15m,1h,1d proxool.simultaneous.build.throttle=30
5.代码下载、编译、打包
代码下载请访问 GitHub上的 witpool/wit-pluto
导入工程文件、编译、打包步骤如下:
6.部署和UT步骤
将编译生成的包wit-rest.war
复制到Tomcat 8.5\webapps\
,重启 tomcat
UT步骤:
(1). 下载Wisdom RESTClient
(2). 双击 JAR包 restclient-1.1.jar
启动工具
导入测试用例文件:
相关推荐
3. 接下来,我们需要实现Restful API和WebService API接口,使用Spring Boot的Restful API和CXF框架来实现学生信息的增删改查操作。 4. 最后,我们需要测试Restful API和WebService API接口,确保其正常工作。 结论...
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
总结来说,这个项目展示了如何利用CXF、Spring和Hibernate这三大框架实现一个完整的Web服务,涵盖了从数据层到服务层再到客户端调用的全过程。对于学习者来说,这是一个很好的实践案例,有助于深入理解这些框架的...
【标题】"cxf+spring+hibernate整合添加功能实现修改版"涉及的是一个集成开发环境中的核心技术栈,即Apache CXF、Spring框架和Hibernate ORM的整合应用,旨在实现服务添加功能的优化。Apache CXF是一个开源的WS-*...
这里少了一个类,是根据实体类生成xml的文件下载地址为:http://download.csdn.net/detail/qq_14996421/9495688
以上是CXF+Spring实现Web Service的基本流程和关键知识点。实际应用中,还需要根据具体的需求和环境进行适当的调整和扩展。例如,如果涉及到大型分布式系统,可能还需要考虑服务治理、负载均衡等问题。通过熟练掌握...
本实例将详细阐述如何利用CXF和Spring来构建Web服务的客户端和服务端。 一、CXF简介 CXF是一个开源的Java框架,专门用于构建和消费Web服务。它支持SOAP、RESTful等多种服务模型,并且可以方便地与Spring框架集成,...
Spring Boot 实现Restful Webservice 服务端示例代码 Spring Boot 是一个基于 Java 的框架,用于快速构建生产级别的应用程序。它提供了许多有用的特性,如自动配置、嵌入式容器、生产准备等。下面,我们将探讨如何...
ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729
总的来说,"cxf+spring的webservice实例"是一个实践性的教程,旨在帮助开发者理解如何在Spring环境中利用CXF快速构建和部署Web服务。通过这个实例,你可以掌握从创建服务到发布、测试的整个流程,进一步提升你的Java...
Apache CXF和Spring提供了丰富的扩展点,可以集成如Spring Security来控制访问权限,使用Spring AOP来处理事务,以及通过CXF的拦截器机制来实现自定义的日志、验证等功能。 总结来说,Apache CXF 2与Spring 2.5的...
"cxf+spring+hibernate集成整合jar包"就是这样一个集合,它将三个关键的技术框架——CXF、Spring和Hibernate整合在一起,为开发人员提供了一个强大的后端服务开发平台。以下是对这些技术及其集成的详细解释。 **CXF...
配置好数据库,放到tomcat访问 http://localhost:8080/Service wsdl:http://localhost:8080/Service/hello?wsdl ...访问rest实例:http://localhost:8080/Service/rest/user/test/1 浏览器输出 hello 1
【标题】:“cxf+spring webservice demo client” 在IT领域,Web服务是一种常见的系统间交互方式,它允许不同应用程序之间共享数据和服务。本示例是关于如何使用Apache CXF和Spring框架创建一个Web服务客户端的...
CXF+Spring+自定义拦截器 webservice源码下载
【标题】"CXF+Spring+Tomcat发布WebService"涉及的是使用Apache CXF框架与Spring框架结合,在Tomcat服务器上部署和消费Web服务的过程。这是一个常见的企业级应用开发场景,特别是对于实现基于SOAP协议的Web服务。...
【标签】"CXF+spring WebService CXF"强调了这些组件的集成,特别是CXF作为Web服务的主要提供者,以及与Spring的紧密配合。CXF不仅可以用作服务提供者,还可以作为客户端来消费其他服务,这在Spring的管理下变得更加...
在这个"**cxf+spring+client**"的主题中,我们将深入探讨CXF与Spring框架结合创建客户端服务的细节,以及如何利用Spring MVC来增强应用程序的交互性。 首先,让我们关注CXF。CXF允许开发者使用Java编程语言来定义和...
web项目使用spring和cxf的一个开发实例,有简单的代码样例和jar。是一个完整的项目,最终发布完成时访问 http://ip:port/项目名称/webservices/ 就会发现你发布的webservice服务。
【CXF+SPRING例子】是一个关于如何将Apache CXF与Spring框架整合的示例项目。Apache CXF是一个开源服务框架,它允许开发者创建和消费Web服务,而Spring框架则是Java应用开发的强大支撑,提供了依赖注入、AOP(面向切...