最近的一个项目用到了restlet+spring+ hibernate配置。查了很多资料,才搞定。记录下。
pom.xml的依赖包:
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-jfreechart-plugin</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>10.2.0.3.0</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>nls_charset12</artifactId>
<version>10.2.0.1.0</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.opensymphony.quartz</groupId>
<artifactId>quartz-all</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.33</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-all</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.33</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jaxrs</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>2.0.6</version>
</dependency>
spring配置:
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-jfreechart-plugin</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>10.2.0.3.0</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>nls_charset12</artifactId>
<version>10.2.0.1.0</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.opensymphony.quartz</groupId>
<artifactId>quartz-all</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.33</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-all</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.33</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jaxrs</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>2.0.6</version>
</dependency>
application-restlet.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Restlet component -->
<bean id="restletComponent" class="org.restlet.Component" />
<!-- All the JAX RS-related entities -->
<context:component-scan base-package="com.fsti.lifeserver.resource" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Service" type="annotation" />
</context:component-scan>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Restlet component -->
<bean id="restletComponent" class="org.restlet.Component" />
<!-- All the JAX RS-related entities -->
<context:component-scan base-package="com.fsti.lifeserver.resource" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Service" type="annotation" />
</context:component-scan>
</beans>
web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/fsti/lifeserver/config/spring/applicationContext-*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>myRESTApi</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<!-- this value must match the bean id of the Restlet component you will configure in Spring (below) -->
<param-value>restletComponent</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myRESTApi</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/fsti/lifeserver/config/spring/applicationContext-*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>myRESTApi</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<!-- this value must match the bean id of the Restlet component you will configure in Spring (below) -->
<param-value>restletComponent</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myRESTApi</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
最后还要一个application
package
com.fsti.lifeserver.resource.application;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.restlet.Component;
import org.restlet.ext.jaxrs.InstantiateException;
import org.restlet.ext.jaxrs.JaxRsApplication;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.restlet.ext.jaxrs.ObjectFactory;
import com.fsti.lifeserver.resource.buddy.BuddyResource;
import com.fsti.lifeserver.resource.category.CategoryResource;
import com.fsti.lifeserver.resource.platlog.LogResource;
import com.fsti.lifeserver.resource.shop.ShopResource;
import com.fsti.lifeserver.resource.user.UserResource;
@org.springframework.stereotype.Component
public final class SpringJaxRsApplication extends JaxRsApplication implements InitializingBean, BeanFactoryAware{
// NOTE: restletComponent instantiation point will be given below (we do it in the spring config)
@Autowired
@Qualifier( " restletComponent " )
private Component component;
/**
* { @inheritDoc }
*/
public void afterPropertiesSet() throws Exception {
setContext(component.getContext().createChildContext());
final Set < Class <?>> classes = new HashSet < Class <?>> ();
// NOTE: Here we must list all the provider JAX RS classes
classes.add(LogResource. class );
classes.add(CategoryResource. class );
classes.add(BuddyResource. class );
classes.add(UserResource. class );
classes.add(ShopResource. class );
add( new Application() {
@Override
public Set < Class <?>> getClasses() { return classes; }
});
component.getDefaultHost().attach( this );
}
public void setBeanFactory( final BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
setObjectFactory( new ObjectFactory() {
public < T > T getInstance(Class < T > jaxRsClass) throws InstantiateException {
// NOTE: We delegated bean instantiation to spring
String str = jaxRsClass.getSimpleName();
return (T) arg0.getBean(str.substring( 0 , 1 ).toLowerCase() + str.substring( 1 ,str.length()));
}
});
}
}
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.restlet.Component;
import org.restlet.ext.jaxrs.InstantiateException;
import org.restlet.ext.jaxrs.JaxRsApplication;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.restlet.ext.jaxrs.ObjectFactory;
import com.fsti.lifeserver.resource.buddy.BuddyResource;
import com.fsti.lifeserver.resource.category.CategoryResource;
import com.fsti.lifeserver.resource.platlog.LogResource;
import com.fsti.lifeserver.resource.shop.ShopResource;
import com.fsti.lifeserver.resource.user.UserResource;
@org.springframework.stereotype.Component
public final class SpringJaxRsApplication extends JaxRsApplication implements InitializingBean, BeanFactoryAware{
// NOTE: restletComponent instantiation point will be given below (we do it in the spring config)
@Autowired
@Qualifier( " restletComponent " )
private Component component;
/**
* { @inheritDoc }
*/
public void afterPropertiesSet() throws Exception {
setContext(component.getContext().createChildContext());
final Set < Class <?>> classes = new HashSet < Class <?>> ();
// NOTE: Here we must list all the provider JAX RS classes
classes.add(LogResource. class );
classes.add(CategoryResource. class );
classes.add(BuddyResource. class );
classes.add(UserResource. class );
classes.add(ShopResource. class );
add( new Application() {
@Override
public Set < Class <?>> getClasses() { return classes; }
});
component.getDefaultHost().attach( this );
}
public void setBeanFactory( final BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
setObjectFactory( new ObjectFactory() {
public < T > T getInstance(Class < T > jaxRsClass) throws InstantiateException {
// NOTE: We delegated bean instantiation to spring
String str = jaxRsClass.getSimpleName();
return (T) arg0.getBean(str.substring( 0 , 1 ).toLowerCase() + str.substring( 1 ,str.length()));
}
});
}
}