- 浏览: 70523 次
- 性别:
- 来自: 杭州
最新评论
-
clampyzoo:
学习了!好帖
Servlet线程安全相关问题 -
jiuyuehe:
有种你写中文给我看看
When Runtime.exec() won't
Reference:http://blogs.sun.com/enterprisetechtips/entry/jersey_and_spring
Jersey is an open-source, production-ready reference implementation of JAX-RS, the Java API for RESTful Web Services (JSR-311). JAX-RS is an annotation-driven API that makes it easy to build Java-based RESTful web services that adhere to the REST architectural style. The JAX-RS API is standardized by the Java Community Process. The JAX-RS API is currently at version 1.0 and Jersey is at version 1.0.2.
Jersey provides additional value beyond the JAX-RS API. It provides its own APIs that support Atom's XML format, MIME MultiPart message format , JavaScript Object Notation (JSON), Web Application Description Language (WADL), as well as Spring framework integration. Jersey is shipped with GlassFish and is available from the GlassFish version 2 and version 3 update centers.
An earlier Tech Tip, Implementing RESTful Web Services in Java, introduced RESTful Web Services, JAX-RS, and Jersey. It also showed how you can write RESTful web services that conform to the JAX-RS specification. Other tips on Jersey-related topics described how to configure JSON for RESTful web services in Jersey 1.0 and how to consume RESTful web services with the Jersey client API.
In this tip, you will learn how to use Jersey's integrated support for Spring, a framework for building and running enterprise Java applications. You'll learn how to configure Spring with Jersey and use Jersey's Spring-related features. The tip assumes that you are familiar with Spring concepts. If not, refer to the Spring Tutorial.
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 the
pom.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 you
created the Maven 2 project for the web application is a pom.xml
file that
represents the Maven project. Replace the contents of the pom.xml
file with
the content shown here.
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>
<!-- End Code Sample -->
Create a Spring Application Context Configuration
: The Spring application context configuration file
specifies an application's configuration for initialization by Spring. You need to create a Spring application context
configuration file for the web application. Create a file applicationContext.xml
and put it in the
src/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 resources
in the Java package example.spring.jersey
. Autowiring is a feature in Spring that allows it to introspect
bean 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 with
the 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 configures Spring 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 the
src/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 instantiated
within the scope of the HTTP request. This highlights a difference between the default scopes for JAX-RS or Jersey
and 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 the
scopes that Jersey supports.
The MyResource
root resource class is functionally equivalent to the root resource class that you originally
created, but it's now Spring-enabled.
After the Spring-enabled web application is successfully deployed, you should see output
similar 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 the
class MyResource
is a root resource class and also a Spring bean class. No Jersey-specific configuration was
required to register root resource classes, as was the case in the web.xml
for the original version of the
web application.
Because Spring is used to register Spring beans, in this case using autowiring, Jersey leverages Spring to perform registration rather that requiring duplicate registration. It is possible to intermix Spring-managed and Jersey-managed root resource classes by using Jersey's registration mechanism. It does not matter if both Spring and Jersey find the same class -- 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 has
its own instance of a Spring bean created from a single Spring bean definition. This scope requires that you declare the Spring
RequestContextListener
servlet context in theweb.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 for each request for that specific bean.
You can inject Jersey artifacts into fields of Spring bean instances according to the scoping rules. If the scope is prototype, then the scoping rules for request apply. If Jersey does not recognize the scope, then it assumes a scope of 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 line window:
<!-- 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 correctly inject 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 to Jersey'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 that
was not already matched by a root resource. This means that you can include a subresource locator method in a resource
to return an instance of a newly created Spring bean class. You use the Jersey
ResourceContext
class to 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.
发表评论
-
When Runtime.exec() won't
2009-12-19 15:51 1863Reference:http://www.javaworld. ... -
Servlet线程安全相关问题
2009-12-10 15:11 4096servlet采用单实例多线程 ... -
IText 生成PDF
2009-07-21 17:14 1313public static writePDF(){ Doc ... -
JSP/Servlet编码问题总结
2009-04-11 11:28 866设置编码的方式分为: ... -
webservice multiple configuration
2009-01-12 09:19 1002apache axis client invoke test ... -
Java Servlet概论
2008-10-15 15:43 1032Java Servlet概论 Servl ... -
JSTL function 标签 使用
2008-09-22 10:19 2867<taglib> <t ... -
论ThreadLocal
2008-08-22 15:23 1459在 Web 应用程序开发中, 我们往往也需要在 Action ... -
Java多线程下载
2008-08-22 10:53 1801public class ExecDownloadFile e ... -
利用commons-fileupload实现多个文件上传
2008-08-08 12:18 22451. <html> 2. < ... -
Spring 2.0 AOP 与事务配置
2008-07-13 17:01 1314Spring 2.0 的重头戏之一就是AspectJ 式 AO ...
相关推荐
标题 "integrating Spring and DWR" 指的是将Spring框架与Direct Web Remoting (DWR)集成的技术主题。DWR是一种开源JavaScript库,它允许Web应用程序与服务器端Java对象进行实时交互,提供了类似Ajax的功能,但更...
《Integrating Voice and Data Networks》是由Cisco Press出版的一本经典教材,主要面向网络管理人员、开发人员以及希望使用Cisco公司的语音和数据集成产品的CCIE(Cisco Certified Internetwork Expert,思科认证...
Expand your imagination by letting go of the limitations of traditional animation mediums, software packages, or workflows and integrating 2D and 3D assets. With the updated and expanded second ...
《通过实例与场景融合实现语义场景完成》 语义场景完成是一种旨在从单视图深度或RGBD图像中精确重建三维场景的像素级语义任务,它是室内场景理解中的关键但具有挑战性的问题。本文提出了一种名为场景-实例-场景网络...
标题 "Integrating Flex, BlazeDS, and Spring Security" 指的是将Adobe Flex与BlazeDS服务以及Spring Security框架整合在一起的技术实践。这通常是为了构建一个富互联网应用程序(RIA,Rich Internet Application)...
Integrating Voice and Data Networks (Keagy, ISBN# 1578701961)
《基于层次And-Or模型的上下文与遮挡在车辆检测中的融合应用》 车辆检测是计算机视觉领域的重要研究方向,广泛应用于智能交通、自动驾驶、安全监控等多个领域。本研究聚焦于如何通过层次And-Or模型来提升车辆检测的...
David Silver RL Lecture 8 Integrating Learning and Planning
Eclipse BIRT主站上推荐的两本权威书籍有两本:1.BIRT: A Field Guide to Reporting 2.Integrating and Extending BIRT 第一本是BIRT报表开发的基本介绍,第二本将如何集成与扩展BIRT应用。这是第二本书的2008第二版
Prentice Hall - Service Oriented Architecture A Field Guide to Integrating XML and Web Services (2004) Prentice Hall - Service Oriented Architecture A Field Guide to Integrating XML and Web Services ...
Towards Integrating Control and Information Theories_ From Information-Theoretic Measures to Control Performance Limitations-2017