CXF+Spring+Hibernate实现RESTful webservice服务端实例

简介: 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 
导入工程文件、编译、打包步骤如下: 
导入工程文件选择Existing Maven Projects
指定要导入的Maven工程的根目录路径
选择主工程 wit-pluto 运行maven install
在wit-rest的target目录下获取wit-rest.war

 6.部署和UT步骤 

将编译生成的包wit-rest.war复制到Tomcat 8.5\webapps\,重启 tomcat 
UT步骤: 
(1). 下载Wisdom RESTClient 
(2). 双击 JAR包 restclient-1.1.jar 启动工具 
导入测试用例文件: 
导入用例文件

目录
相关文章
|
1月前
|
Java API 数据库
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
48 4
|
1月前
|
Java API 数据库
如何使用Spring Boot构建RESTful API,以在线图书管理系统为例
【10月更文挑战第9天】本文介绍了如何使用Spring Boot构建RESTful API,以在线图书管理系统为例,从项目搭建、实体类定义、数据访问层创建、业务逻辑处理到RESTful API的实现,详细展示了每个步骤。通过Spring Boot的简洁配置和强大功能,开发者可以高效地开发出功能完备、易于维护的Web应用。
61 3
|
2月前
|
缓存 Java 应用服务中间件
随着微服务架构的兴起,Spring Boot凭借其快速开发和易部署的特点,成为构建RESTful API的首选框架
【9月更文挑战第6天】随着微服务架构的兴起,Spring Boot凭借其快速开发和易部署的特点,成为构建RESTful API的首选框架。Nginx作为高性能的HTTP反向代理服务器,常用于前端负载均衡,提升应用的可用性和响应速度。本文详细介绍如何通过合理配置实现Spring Boot与Nginx的高效协同工作,包括负载均衡策略、静态资源缓存、数据压缩传输及Spring Boot内部优化(如线程池配置、缓存策略等)。通过这些方法,开发者可以显著提升系统的整体性能,打造高性能、高可用的Web应用。
75 2
|
3月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
92 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
3月前
|
Java API 数据库
【神操作!】Spring Boot打造RESTful API:从零到英雄,只需这几步,让你的Web应用瞬间飞起来!
【8月更文挑战第12天】构建RESTful API是现代Web开发的关键技术之一。Spring Boot因其实现简便且功能强大而深受开发者喜爱。本文以在线图书管理系统为例,展示了如何利用Spring Boot快速构建RESTful API。从项目初始化、实体定义到业务逻辑处理和服务接口实现,一步步引导读者完成API的搭建。通过集成JPA进行数据库操作,以及使用控制器类暴露HTTP端点,最终实现了书籍信息的增删查改功能。此过程不仅高效直观,而且易于维护和扩展。
61 1
|
3月前
|
Java 数据库连接 数据库
Spring Data JPA 与 Hibernate 之区别
【8月更文挑战第21天】
87 0
|
4月前
|
Java 数据库连接 数据库
如何在Spring Boot中集成Hibernate
如何在Spring Boot中集成Hibernate
|
4月前
|
Java API 数据库
使用Spring Boot构建RESTful API
使用Spring Boot构建RESTful API
|
4月前
|
开发框架 Java API
使用Spring Boot构建RESTful API的最佳实践
使用Spring Boot构建RESTful API的最佳实践
|
4月前
|
Java API Spring
Spring Boot中的RESTful API版本控制
Spring Boot中的RESTful API版本控制