Building RESTful services with Jersey+spring+hibernate

Source: Internet
Author: User
Tags glassfish netbeans

Recently combed the rest of the writing before the end of the service, because previously written in NetBeans, the time has long felt netbeans is really ka, card!!!

Now use Eclipse to re-comb, now to record the overall structure, quickly build a basic project service architecture.

First, create a dynamic WEB projects project with the project structure such as:

Second, import the required jar package, jar (Https://pan.baidu.com/s/1eSA3zPo)

Third, configuration Restapplication.java file

Import Org.codehaus.jackson.jaxrs.JacksonJsonProvider;
Import Org.glassfish.jersey.server.ResourceConfig;

public class Restapplication extends ResourceConfig {
Public Restapplication () {

The package path where the service class resides
Packages ("Resources package path");
Registering a JSON converter
Register (Jacksonjsonprovider.class);

}
}

Iv. Configuring the Applicationcontext.xml file

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"

Xsi:schemalocation= "Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-4.0.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

Http://www.springframework.org/schema/tx

Http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

<context:annotation-config></context:annotation-config>

<context:component-scan base-package= "Com.zh.rest.service"/>

<context:component-scan base-package= "Com.zh.rest.dao"/>

<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

<property name= "Locations" >

<list>

<value>classpath:jdbc.properties</value>

</list>

</property>

</bean>

<bean id= "C3p0datasource" destroy-method= "Close"

class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >

<property name= "Driverclass" value= "${driverclass}"/>

<property name= "Jdbcurl" value= "${url}"/>

<property name= "user" value= "${username}"/>

<property name= "Password" value= "${password}"/>

<property name= "initialpoolsize" value= "${c3p0.pool.initialpoolsize}"/>

<property name= "minpoolsize" value= "${c3p0.pool.minpoolsize}"/>

<property name= "maxpoolsize" value= "${c3p0.pool.maxpoolsize}"/>

<property name= "MaxIdleTime" value= "${c3p0.pool.maxidletime}"/>

</bean>

<bean id= "Sessionfactory"

class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >

<property name= "DataSource" ref= "C3p0datasource"/>

<property name= "Packagestoscan" >

<list>

<value>com.zh.rest.model</value>

</list>

</property>

<property name= "Hibernateproperties" >

<props>

<prop key= "Hibernate.dialect" >${hibernate.dialect}</prop>

<prop key= "Hibernate.hbm2ddl.auto" >${hibernate.hbm2ddl.auto}</prop>

<prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop>

<prop key= "Hibernate.format_sql" >${hibernate.format_sql}</prop>

<prop key= "Hibernate.hbm2ddl.auto" >${hibernate.hbm2ddl.auto}</prop>

</props>

</property>

</bean>

<bean id= "Txmanager"

class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >

<property name= "Sessionfactory" ref= "Sessionfactory"/>

</bean>

<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >

<tx:attributes>

<tx:method name= "add*" propagation= "REQUIRED"/>

<tx:method name= "save*" propagation= "REQUIRED"/>

<tx:method name= "update*" propagation= "REQUIRED"/>

<tx:method name= "delete*" propagation= "REQUIRED"/>

<tx:method name= "modify*" propagation= "REQUIRED"/>

<!--Hibernate4 must be configured to turn on transactions otherwise getcurrentsession () cannot get

<tx:method name= "*" propagation= "REQUIRED" read-only= "true"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id= "bizmethods" expression= "Execution (* com.zh.rest.service.*.* (..))"/>

<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Bizmethods"/>

</aop:config>

</beans>

V. Configuring the Web. xml file

<?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"

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>AHRESTful</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

<module-name>AHRESTful</module-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

Classpath:applicationContext.xml

</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>way REST service</servlet-name>

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>javax.ws.rs.Application</param-name>

<param-value>com.zh.rest.RestApplication</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>way REST service</servlet-name>

<url-pattern>/app/rest/*</url-pattern>

</servlet-mapping>

</web-app>

Vi. Configuring database Connection Files Jdbc.properties

Driverclass = Com.microsoft.sqlserver.jdbc.SQLServerDriver

URL = jdbc:sqlserver://Database address: Database port; databasename=restdemo

Username = Database Logon name

Password = Database Password

Hibernate.dialect = Com.zh.rest.util.SqlServer2008Dialect

Hibernate.hbm2ddl.auto = True

Hibernate.show_sql = True

Hibernate.format_sql = True

Hibernate.hbm2ddl.auto = Validate

C3p0.pool.maxpoolsize=30

C3p0.pool.minpoolsize=3

C3p0.pool.initialpoolsize=5

C3p0.pool.acquireincrement=3

C3p0.pool.automatictesttable=c3p0testtable

C3p0.pool.testconnectiononcheckin=true

c3p0.pool.idleconnectiontestperiod=18000

c3p0.pool.maxidletime=25000

C3p0.pool.testconnectiononcheckout=true

C3p0.pool.autocommitonclose=false

The relevant configuration is now complete.

Because the project has some configuration problems, if you need a complete project demo, please leave a message, send a separate share

Building RESTful services with Jersey+spring+hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.