Integrating Jersey and Spring(Jersey与Spring整合)
<!-- End Code Sample -->
In response, Maven 2 will prompt you to choose an archetype, that is, a Maven 2 project template, from the archetypeslisted in the archetype catalog, archetype-catalog.xml, at URL http://download.java.net/maven/2:
Choose archetype: 1: http://download.java.net/maven/2 -> jersey-quickstart-grizzly (Archetype for creating a RESTful web application with Jersey and Grizzly) 2: http://download.java.net/maven/2 -> jersey-quickstart-webapp (Archetype for creating a Jersey based RESTful web application WAR packaging) Choose a number: (1/2):<!-- End Code Sample -->
Choose 2, jersey-quickstart-webapp. You will then be prompted for a group ID and an artifact ID. Enterexample.jersey.spring for the group ID and example-spring-jersey for the artifact ID.Accept the default values for the other prompts.
After you confirm the inputs, Maven 2 creates a new subdirectory called example-spring-jersey, which containsa template for the new Jersey-based web application. Figure 1 shows the expanded structure of theexample-spring-jersey directory.

Figure 1. Expanded Structure of the example-spring-jersey directory
Maven 2 also creates a Project Object Model (POM) file, pom.xml, which contains an XML representation of theMaven project. You can find the pom.xml file in the example-spring-jersey directory.If you navigate below the example-spring-jersey directory tosrc/main/java/example/jersey/spring, you'll see a Java class named MyResource thatrepresents a resource used in the application. You'll also find a web.xml file for the web applicationin the src/main/webapp/WEB-INF directory.
Let's build, deploy, and test the web application to see if it works. To build the application, go to theexample-spring-jersey directory and enter the following command:
mvn clean install<!-- End Code Sample -->
In response, Maven 2 compiles the source code and creates an example-spring-jersey.war filefor the application.
To deploy the application, GlassFish V3 Prelude must be running. Start GlassFish V3 Prelude if it isn't already running.To start GlassFish V3 Prelude, enter the following command:
<!-- Start Code Sample --><GF_install_dir/bin>asadmin start-domain domain1<!-- End Code Sample -->
where <GF_install_dir/bin> is the directory where you installed GlassFish v3 Prelude.
Deploy the application to GlassFish v3 Prelude using the following command:
<!-- Start Code Sample -->asadmin deploy --force=true target/example-spring-jersey.war<!-- End Code Sample -->
Finally, you can verify that the deployed application runs by using the command line tool,curl, as follows:
<!-- Start Code Sample -->curl -v http://localhost:8080/example-spring-jersey/webresources/myresource<!-- End Code Sample -->
In response, you should see the following output in the command window:
<!-- Start Code Sample -->* About to connect() to localhost port 8080 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /example-spring-jersey/webresources/myresource HTTP/1.1 > User-Agent: curl/7.17.1 (i586-pc-mingw32msvc) libcurl/7.17.1 OpenSSL/0.9.7c zib/1.2.3 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < X-Powered-By: Servlet/2.5 < Server: Sun Java System Application Server 9.1_02 < Content-Type: text/plain < Transfer-Encoding: chunked < Date: Thu, 02 Apr 2009 22:18:29 GMT < Hi there!* Connection #0 to host localhost left intact * Closing connection #0<!-- End Code Sample -->
You can also verify the application by pointing your browser to theURL http://localhost:8080/example-spring-jersey/webresources/myresource. Youshould see the following text displayed on the page: Hi there!
?
下面是Jersey集成Spring的配置:
?
Transforming the Web Application to Use Spring
Now let's change the web application to use Spring. To do that, you need to take the following actions:
Modify thepom.xml file to include Spring dependencies.Create a Spring application context configuration file.Modify the web.xml file to declare the Spring configuration.Modify the root resource class to be a Spring component.Modify the pom.xml File: Recall that one of the files generated when youcreated the Maven 2 project for the web application is a pom.xml file thatrepresents the Maven project. Replace the contents of the pom.xml file withthe content shown here.
新的pom代码我贴在下面了。注意
<jersey-version>1.0.2</jersey-version>表示jersey的配置verson,可以在http://download.java.net/maven/2/com/sun/jersey/contribs/jersey-spring/找到最新的配置版本。我当前用的是
<jersey-version>1.4</jersey-version>
?
?
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>example.jersey.spring</groupId> <artifactId>example-spring-jersey</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>example-spring-jersey Jersey Webapp</name> <build> <finalName>example-spring-jersey</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <inherited>true</inherited> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>${jersey-version}</version> </dependency> <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-spring</artifactId> <version>${jersey-version}</version> </dependency> </dependencies> <properties> <jersey-version>1.0.2</jersey-version> </properties> <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Maven 2 Repository</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> <repository> <id>maven-repository.dev.java.net</id> <name>Java.net Maven 1 Repository (legacy)</name> <url>http://download.java.net/maven/1</url> <layout>legacy</layout> </repository> </repositories></project>?
The replacing code simplifies the Maven configuration to only use the required dependencies for this example.Note especially the following code, which adds the Jersey Spring dependency, using the jersey-spring module:
<!-- Start Code Sample --> <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-spring</artifactId> <version>${jersey-version}</version> </dependency>?
?
Create a Spring Application Context Configuration: The Spring application context configuration filespecifies an application's configuration for initialization by Spring. You need to create a Spring application contextconfiguration file for the web application. Create a file applicationContext.xml and put it in thesrc/main/resources directory. The file should have the following content:
<!-- Start Code Sample -->
<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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="example.jersey.spring"/> </beans>
<!-- End Code Sample -->
This configuration directs Spring to use autowiring with Spring-based annotations and to scan for Spring-based resourcesin the Java package example.spring.jersey. Autowiring is a feature in Spring that allows it to introspectbean classes for dependencies so that you do not have to explicitly specify bean properties or constructor arguments.
?
Modify the web.xml File: Replace the contents of the web.xml file withthe content shown here.
The following code in the updated web.xml file declares the Spring application context configuration,created earlier as a servlet context parameter:
<!-- Start Code Sample -->
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<!-- End Code Sample -->
The updated content also declares two listeners. The first configures Spring and the second configuresSpring for use with the request scope for Spring beans.
<!-- Start Code Sample -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
<!-- End Code Sample -->
Then, the file declares the Jersey Spring servlet, which supports the Jersey integration with Spring.
<!-- Start Code Sample -->
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> </servlet>
<!-- End Code Sample -->
Modify the Root Resource Class: Modify the MyResource.java file in thesrc/main/java/example/jersey/spring directory with the following content:
<!-- Start Code Sample -->
package example.jersey.spring; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; // The Java class will be hosted at the URI path "/myresource" @Path("/myresource") @Component @Scope("request") public class MyResource { // The Java method will process HTTP GET requests @GET // The Java method will produce content identified by the MIME Media // type "text/plain" @Produces("text/plain") public String getIt() { return "Hi there!"; } }<!-- End Code Sample -->
The @Component annotation declares that the class is a Spring bean class.The @Scope("request") annotation declares that instances of this class will be instantiatedwithin the scope of the HTTP request. This highlights a difference between the default scopes for JAX-RS or Jerseyand Spring. The default scope for JAX-RS is per request. By comparison, the default scope for Spring is a singleton,that is, one instance per web application. See Supported Scopes for more information about thescopes that Jersey supports.
The MyResource root resource class is functionally equivalent to the root resource class that you originallycreated, but it's now Spring-enabled.
?
After the Spring-enabled web application is successfully deployed, you should see outputsimilar to the following in the server.log(<GF_install_dir>/glassfish/domains/domain1/logs/server.txt):
<!-- Start Code Sample -->
[PWC1412: WebModule[/example-spring-jersey] ServletContext.log():Initializing Spring root WebApplicationContext|#] [INFO| Root WebApplicationContext: initialization started|] [INFO| Refreshing org.springframework.web.context.support.XmlWebApplicationContext@53ecec: display name [Root WebApplicationContext]; startup date [Mon Apr 06 14:37:02 PDT 2009]; root of context hierarchy|#] [INFO| Loading XML bean definitions from class path resource [applicationContext.xml]|#] [INFO| Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@53ecec]: org.springframework.beans.factory.support.DefaultListableBeanFactory@c0267a|#] [INFO| Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c0267a: defining beans [myResource,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy|#] [INFO| Root WebApplicationContext: initialization completed in 891 ms|#] [INFO| Loading application example-spring-jersey at /example-spring-jersey|#] [INFO| Deployment of example-spring-jersey done is 4500 ms|#] [INFO| Registering Spring bean, myResource, of type example.jersey.spring.MyResource as a root resource class|#]
<!-- End Code Sample -->
Notice the final line that begins "Registering Spring bean". This is output from Jersey. Jersey knows that theclass MyResource is a root resource class and also a Spring bean class. No Jersey-specific configuration wasrequired to register root resource classes, as was the case in the web.xml for the original version of theweb application.
Because Spring is used to register Spring beans, in this case using autowiring, Jersey leverages Spring to performregistration rather that requiring duplicate registration. It is possible to intermix Spring-managed and Jersey-managedroot resource classes by using Jersey's registration mechanism. It does not matter if both Spring and Jersey find the sameclass -- only one reference to the class will be managed appropriately.
Supported Scopes
Jersey supports the following Spring scopes:
Request. Scopes a single Spring bean definition to the lifecycle of a single HTTP request, that is, each HTTP request hasits own instance of a Spring bean created from a single Spring bean definition. This scope requires that you declare the SpringRequestContextListener servlet context in the web.xml file for the web application.Singleton. Scopes a single Spring bean definition to a single object instance per web application.Prototype. Scopes a single Spring bean definition to multiple object instances. A new Spring bean instance is created foreach request for that specific bean.You can inject Jersey artifacts into fields of Spring bean instances according to the scoping rules. If the scope isprototype, then the scoping rules for request apply. If Jersey does not recognize the scope, then it assumes a scopeof singleton for the purpose of injection.
For example, you can modify the MyResource class in the Spring-enabled Web application to include an@QueryParam for injection. Here is what the modified class looks like:
<!-- Start Code Sample -->
package example.jersey.spring; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; // The Java class will be hosted at the URI path "/myresource" @Path("/myresource") @Component @Scope("request") public class MyResource { @QueryParam("x") String x; // The Java method will process HTTP GET requests @GET // The Java method will produce content identified by the MIME Media // type "text/plain" @Produces("text/plain") public String getIt() { return "Hi there! " + x; } }<!-- End Code Sample -->
In response, Jersey should inject the information into the Spring bean in request scope, that is, per request.To test that, you can redeploy and execute the updated application by entering the following commands in a command linewindow:
<!-- Start Code Sample -->
mvn clean install asadmin deploy --force=true target/example-spring-jersey.war curl -v http://localhost:8080/example-spring-jersey/webresources/myresource?x=curl
<!-- End Code Sample -->
The application should return "Hi there! curl" in the output. If it does, this verifies that Jersey can correctlyinject information into the Spring bean per request.
Summary
This tip showed you how to use some of Jersey's Spring-related features. But there are other useful elements toJersey's support for Spring. Some of the these are:
You can take advantage of the JAX-RS support for hierarchical URI path matching to further match a URI path thatwas not already matched by a root resource. This means that you can include a subresource locator method in a resourceto return an instance of a newly created Spring bean class. You use the JerseyResourceContext classto obtain the instance.You can inject Spring beans into JAX-RS-based methods. You do this with the Jersey @Inject annotation.You can use Jersey Spring-based Aspect-Oriented Programming (AOP).You can have Spring instantiate a resource class, but have Jersey manage the resource class's lifecycle. 1 楼 yaoyaozii 2011-10-05 假设有UsersResource