Monday Apr 13, 2009
Monday Apr 13, 2009
by Paul Sandoz
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.
Creating a Basic Web Application
To demonstrate Jersey's Spring-related features, you'll first create a simple web application, one that does not use Spring and then change it to use Spring. Let's use the Maven 2 software project management tool to build the simple web application. If you're not familiar with Maven, see Welcome to Maven and Building Web Applications with Maven 2.
First, create a Maven 2 project by running the following Maven 2 archetype plugin in a command line:
In response, Maven 2 will prompt you to choose an archetype, that is, a Maven 2 project template, from the archetypes
listed in the archetype catalog, archetype-catalog.xml, at URL http://download.java.net/maven/2:
Choose 2, jersey-quickstart-webapp. You will then be prompted for a group ID and an artifact ID. Enter
example.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 contains
a template for the new Jersey-based web application. Figure 1 shows the expanded structure of the
example-spring-jersey directory.
|
|
Figure 1. Expanded Structure of the |
Maven 2 also creates a Project Object Model (POM) file, pom.xml, which contains an XML representation of the
Maven project. You can find the pom.xml file in the example-spring-jersey directory.
If you navigate below the example-spring-jersey directory to
src/main/java/example/jersey/spring, you'll see a Java class named MyResource that
represents a resource used in the application. You'll also find a web.xml file for the web application
in 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 the
example-spring-jersey directory and enter the following command:
In response, Maven 2 compiles the source code and creates an example-spring-jersey.war file
for 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:
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:
Finally, you can verify that the deployed application runs by using the command line tool, curl, as follows:
In response, you should see the following output in the command window:
You can also verify the application by pointing your browser to the URL http://localhost:8080/example-spring-jersey/webresources/myresource. You should see the following text displayed on the page: Hi there!
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:
pom.xml file to include Spring dependencies.web.xml file to declare the Spring configuration.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:
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:
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:
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.
Then, the file declares the Jersey Spring servlet, which supports the Jersey integration with Spring.
Modify the Root Resource Class: Modify the MyResource.java file in the
src/main/java/example/jersey/spring directory with the following content:
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.
Verify that the Spring-enabled web application deploys and executes by entering the following commands in a command line:
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):
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:
RequestContextListener servlet context in the web.xml file for the web application.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:
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:
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:
ResourceContext class
to obtain the instance.
@Inject annotation.
Further Reading
For more information on Jersey and Spring, see the following resources:
ResourceContext Interface@Inject AnnotationAbout the Author
Paul Sandoz is the co-spec lead and implementation lead for JSR 311: Java API for RESTful Web Services. He has participated in the W3C, ISO, and ITU-T standards organizations and contributed various performance-related technologies and improvements to the GlassFish web services stack, particularly in standardization, implementation, integration, and interoperability of Fast Infoset.
2009 JavaOne Conference, June 2-5, San Francisco ** Register Now**
Stay on top of everything new and different, both inside and around Java technology. Register by April 22, 2009, and save $200 off a Conference Pass or Conference Plus Pass. Register now at http://java.sun.com/javaone.
For developers wishing to avoid the dreaded command line it is possible to utilize NetBeans 6.5 with the maven plugin to deploy the project to Glassfish V3 Prelude. And of course, as stated, the URLs can be accessed using the browser instead of curl.
Posted by Paul Sandoz on April 14, 2009 at 01:06 AM PDT #
Thanks, paul,It is a good article!
Posted by redhacker on April 15, 2009 at 12:32 AM PDT #
It is a good article. Thanks!
Posted by 204.176.49.46 on April 28, 2009 at 06:07 PM PDT #
Est-il possible d'avoir la traduction française ? Thanks
Posted by 92.150.71.65 on April 29, 2009 at 12:20 PM PDT #
Mon français n'est pas assez bon. J'ai demandé à quelqu'un qui pourrait le savoir. Mais, peut-être que quelqu'un peut offrir ses services?
Posted by Paul Sandoz on April 30, 2009 at 08:35 AM PDT #
Is it possible to post the pom.xml for maven?
Posted by Em on June 04, 2009 at 07:16 AM PDT #
@Em: See the following for a maven-based Spring sample:
http://download.java.net/maven/2/com/sun/jersey/samples/spring-annotations/1.1.0-ea/spring-annotations-1.1.0-ea-project.zip
Paul.
Posted by Paul Sandoz on June 05, 2009 at 02:00 PM PDT #
thx Paul.
Can I return a ModelAndView - Object instead of a String?
In my example I get this error: A message body writer for Java type, class org.springframework.web.servlet.ModelAndView, and MIME media type, text/html, was not found. The InternalResourceViewResolver is configured.
Posted by Em on June 08, 2009 at 05:49 AM PDT #
@Em, apologies for the late reply.
Integration with Spring's model/view is not currently implemented.
You will need to add your own MessageBodyWriter to support the processing of the ModelAndView instance.
I recommend we continue discussions on:
mailto:users@jersey.dev.java.net
as it is easier to have a conversation.
Paul.
Posted by Paul Sandoz on June 11, 2009 at 12:47 AM PDT #
Thanks Paul,
I implemented your implementation and it worked. The only problem is that it wouldn't integrate with Spring's JPA which doesn't lead to a very useful REST application. I'm begining to think that although Annotations are a cool idea when it comes to debugging they are a nightmare.
Funny its hard to find an exemplar of Spring JPA with Jeresy. I suppose it will have to be wall to wall Spring implementations in the end with Spring 3.0. I would however prefer a bit more diversity.
David
Posted by David on July 24, 2009 at 09:21 PM PDT #
I do not know anything about Spring JPA. Can you describe what did not work with Spring JPA? perhaps you can email:
mailto:users@jersey.dev.java.net
then i and others can discuss.
Re: annotations. Good error reporting is a must, we have tried to do that in Jersey so it reports all errors about resource classes, rather than one at a time. Tooling can also help a lot. IMHO if i were to choose between an XML document or annotations i would choose the latter. I cannot imagine the PITA it would be to use an XML document rather than annotations for JAX-RS.
Paul.
Posted by Paul Sandoz on July 27, 2009 at 12:38 AM PDT #
I will thank u, for this very important and nice article here!
Posted by Ankara Bilgisayar, Bilgisayar Satış, Teknoloji Mağazası on August 08, 2009 at 02:02 PM PDT #
Thanks very much for the quick tutorial. FYI I had to modify my web.xml to point to the jersey/spring servlet like so
<servlet>
<servlet-name>Jersey Spring</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Spring</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
Posted by Matthew Sowders on September 25, 2009 at 04:14 PM PDT #
Yes, this is covered in the section "Modify the web.xml File".
Posted by Edward Ort on September 29, 2009 at 08:30 AM PDT #
Thank you for this article.
Works fine when using spring 2.5.6 in the project. But there seems to be a problem when trying to run with spring 3.0.0.RC1.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/xxx/dao/impl/JpaAuditDAOTest-context.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
Posted by Roger Nilsson on October 27, 2009 at 12:43 AM PDT #