CXF+Spring+Hibernate实现RESTful webservice服务端实例
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 启动工具
导入测试用例文件:
CXF+Spring+Hibernate实现RESTful webservice服务端实例的更多相关文章
- Spring Boot 实现RESTful webservice服务端实例
1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...
- Spring Boot 实现RESTful webservice服务端示例
1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...
- 基于CXF框架下的SOAP Webservice服务端接口开发
最近对webservice 进行入门学习,网上也是找了很多的学习资料.总得感觉就是这了解点,那了解点.感觉不够系统,不够容易入门.差不多断断续续看了一个星期了,今天小有成果,把客户端,服务端都搞定了. ...
- 根据wsdl反向生成webservice服务端(3种方法)
前言 正常情况下,都是我们项目组创建一个webservice服务端,客户通过我们提供的wsdl地址生成客户端并进行访问:但是最近和一个国企做接口对接,他们却只提供给我们wsdl,需要我们根据wsdl生 ...
- JAVA WEBSERVICE服务端&客户端的配置及调用(基于JDK)
前言:我之前是从事C#开发的,因公司项目目前转战JAVA&ANDROID开发,由于对JAVA的各种不了解,遇到的也是重重困难.目前在做WEBSERVICE提供数据支持,看了网上相关大片的资料也 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...
- myeclipse-建立webservice服务端和客户端
一.建立webservice服务端: 1.新建一个web service project,名称为webservice_server截图如下,点击finish. 2.选择工程,点击右键,选择new-&g ...
- DelphiXE7中创建WebService(服务端+客户端)
相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...
随机推荐
- Java中的字段和属性
Java中的属性,通常可以理解为get和set方法.而字段,通常叫做“类成员”. 属性只局限于类中方法的声明,并不与类中其他成员相关.例如:void setA(String s){}String ge ...
- Spark中的partition和block的关系
hdfs中的block是分布式存储的最小单元,类似于盛放文件的盒子,一个文件可能要占多个盒子,但一个盒子里的内容只可能来自同一份文件.假设block设置为128M,你的文件是250M,那么这份文件占3 ...
- shell实现SSH自动登陆【转】
前言 公司开发使用docker,每次登陆自己开发机总要输入 ssh user_name@ip_string,然后再确认输入password,手快了还经常会输错.作为一个懒人,肯定要找一个取巧的方式,查 ...
- selenium + python 测试环境搭建 (WINDOWS)
1. 下载Python , 运行.exe -> http://python.org/getit/ 2. 下载Python Setuptools 基础包管理工具安装,官方文档参考 https:// ...
- Permission denied: user=root, access=WRITE, inode="/":hadoopuser:supergroup:drwxr-xr-x
提示往HDFS写文件是不容许的. 在conf/hdfs-site.xml中加入: <property> <name>dfs.permissions</name> & ...
- python mysql program
//test.py #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect(&q ...
- Response.Redirect & window.location.href
对接中信的微信H5支付时,对方(其实是微信)需要对我们的域名进行授权,即,我方需向渠道报备支付域名,微信只认可由此域名发起的支付交易. 支付中心只提供了一套支付接口供下游系统访问.因为给渠道报备的域名 ...
- Sift中尺度空间、高斯金字塔、差分金字塔(DOG金字塔)、图像金字塔
转自:https://blog.csdn.net/dcrmg/article/details/52561656 一. 图像金字塔 图像金字塔是一种以多分辨率来解释图像的结构,通过对原始图像进行多尺度像 ...
- PHP json_encode/json_decode与serialize/unserializ性能测
PHP里面,有时候出于实际需求考虑,需要将某些信息以数组的方式进行存储,甚至有时候介于数组.字符串两者之间,很难确定是数组还是字符串,如果最终还需要将这些信息存储到文件系统中,而且要保证正确无误的存储 ...
- tp5 auth权限设置
案例:把所需要的auth类放在一个公共的地方引用,我这里就只是放在了与application同级的extend里面的org所以我在公共控制器里面实例为use \org\Auth; 然后我再公共控制器里 ...