Last active
December 21, 2015 00:48
-
-
Save chocotan/6222834 to your computer and use it in GitHub Desktop.
jersey2 with spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.loli.askloli.spring; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.server.spring.scope.RequestContextFilter; | |
public class MyApplication extends ResourceConfig { | |
public MyApplication () { | |
register(RequestContextFilter.class); | |
this.packages("io.loli.askloli"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>io.loli</groupId> | |
<artifactId>askloli</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<name>askloli</name> | |
<description>一个自定义问卷调查的webapp</description> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<spring.version>3.2.2.RELEASE</spring.version> | |
<java.version>1.7</java.version> | |
<junit.version>4.11</junit.version> | |
<openjpa.version>2.2.2</openjpa.version> | |
<dbcp.version>20030825.184428</dbcp.version> | |
<pool.version>20030825.183949</pool.version> | |
<mysql-driver.version>5.1.25</mysql-driver.version> | |
<log4j.version>1.2.15</log4j.version> | |
<validation.version>1.1.0.Final</validation.version> | |
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version> | |
<jersey.version>2.2-SNAPSHOT</jersey.version> | |
</properties> | |
<repositories> | |
<repository> | |
<id>snapshot-repository.java.net</id> | |
<name>Java.net Snapshot Repository for Maven</name> | |
<url>https://maven.java.net/content/repositories/snapshots/</url> | |
<layout>default</layout> | |
</repository> | |
</repositories> | |
<dependencies> | |
<!-- Spring Dependencies --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>${spring.version}</version> | |
<!-- <exclusions> Exclude Commons Logging in favor of SLF4j <exclusion> | |
<groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> | |
</exclusion> </exclusions> --> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>${spring.version}</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>${spring.version}</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>${spring.version}</version> | |
<type>jar</type> | |
<scope>test</scope> | |
</dependency> | |
<!-- DB Dependencies --> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>${dbcp.version}</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-pool</groupId> | |
<artifactId>commons-pool</artifactId> | |
<version>${pool.version}</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
<!-- View Dependencies --> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.1.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-server</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.ext</groupId> | |
<artifactId>jersey-spring3</artifactId> | |
<version>2.2-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<artifactId>jersey-container-servlet</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<!-- <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> | |
<version>1.1-ea</version> <scope>provided</scope> </dependency> --> | |
<dependency> | |
<groupId>javax.ws.rs</groupId> | |
<artifactId>javax.ws.rs-api</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-client</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<!-- Bean Validation Dependencies --> | |
<dependency> | |
<groupId>javax.validation</groupId> | |
<artifactId>validation-api</artifactId> | |
<version>${validation.version}</version> | |
<scope>compile</scope> | |
</dependency> | |
<!-- JPA Persistence Dependencies --> | |
<dependency> | |
<groupId>org.apache.openjpa</groupId> | |
<artifactId>openjpa</artifactId> | |
<version>${openjpa.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>${mysql-driver.version}</version> | |
</dependency> | |
<!-- JSR-330 --> | |
<dependency> | |
<groupId>javax.inject</groupId> | |
<artifactId>javax.inject</artifactId> | |
<version>1</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>io.loli.askloli</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-eclipse-plugin</artifactId> | |
<version>2.8</version> | |
<configuration> | |
<wtpversion>2.0</wtpversion> | |
<downloadSources>true</downloadSources> | |
<downloadJavadocs>true</downloadJavadocs> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-source-plugin</artifactId> | |
<executions> | |
<execution> | |
<id>attach-sources</id> | |
<goals> | |
<goal>jar</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-javadoc-plugin</artifactId> | |
<executions> | |
<execution> | |
<id>attach-javadocs</id> | |
<goals> | |
<goal>jar</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.tomcat.maven</groupId> | |
<artifactId>tomcat7-maven-plugin</artifactId> | |
<version>2.0</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.loli.askloli.test; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
@Path("/test") | |
@Component | |
public class Test { | |
@Autowired | |
private TestBean testBean; | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public String test(){ | |
testBean.say(); | |
return "hello"; | |
} | |
public TestBean getTestBean() { | |
return testBean; | |
} | |
public void setTestBean(TestBean testBean) { | |
this.testBean = testBean; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | |
version="3.1"> | |
<context-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>/WEB-INF/applicationContext.xml</param-value> | |
</context-param> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener | |
</listener-class> | |
</listener> | |
<listener> | |
<listener-class>org.springframework.web.context.request.RequestContextListener | |
</listener-class> | |
</listener> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener | |
</listener-class> | |
</listener> | |
<servlet> | |
<servlet-name>javax.ws.rs.core.Application</servlet-name> | |
<servlet-class>org.glassfish.jersey.servlet.ServletContainer | |
</servlet-class> | |
<init-param> | |
<param-name>javax.ws.rs.Application</param-name> | |
<param-value>io.loli.askloli.spring.MyApplication</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>javax.ws.rs.core.Application</servlet-name> | |
<url-pattern>/*</url-pattern> | |
</servlet-mapping> | |
<filter> | |
<filter-name>characterEncodingFilter</filter-name> | |
<filter-class>org.springframework.web.filter.CharacterEncodingFilter | |
</filter-class> | |
<init-param> | |
<param-name>encoding</param-name> | |
<param-value>UTF-8</param-value> | |
</init-param> | |
<init-param> | |
<param-name>forceEncoding</param-name> | |
<param-value>true</param-value> | |
</init-param> | |
</filter> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment