Jersey integrates spring and uses the grizzly built-in container test jersey

Source: Internet
Author: User

Jersey integrated Spring is relatively simple, the points to note are:

1. The introduction of Maven dependency jersey-spring, which contains spring dependencies, can be removed by exclusions if it conflicts with the spring version used in the project.

2, ensure that the Jersey resource class is located in the package path, can be scanned by spring, that is, the spring configuration <context:component-scan base-package= ""/>.

3. Make sure the Jersey resource class is annotated @component so that you can inject an object instance of the spring container.

4. Configure Com.sun.jersey.spi.spring.container.servlet.SpringServlet in Web. Xml.

5, Jersey integrated grizzly built-in container test, see Jersey integrated Grizzly. Slightly different in this project, customizing a Myjerseyservlet, inheriting from Springservlet, implementing the spring container loading so that it can rely on injection in the Jersey resource class. This allows you to use the built-in container test.

Project Source Code Reference

Jersey-spring's maven dependency, which removes spring-related dependencies. Specific configuration, you can enter the above "project source code reference".

<!-- jersey-spring:  includes Jersey-servlet/jersey-server/jersey-core, and it also includes spring dependent dependencies.  --><dependency>    <groupId>com.sun.jersey.contribs</groupId>     <artifactid>jersey-spring</artifactid>    <version >1.19</version>    <exclusions>         <exclusion>            <artifactid >spring-core</artifactId>            < Groupid>org.springframework</groupid>        </exclusion >        <exclusion>             <artifactId>spring-beans</artifactId>             <groupid>org.springframework</groupid>         </exclusion>        <exclusion>             <artifactId>spring-context</artifactId>             <groupid>org.springframework</ groupid>        </exclusion>         <exclusion>             <artifactId>spring-web</artifactId>             <groupId>org.springframework</groupId>         </exclusion>        <exclusion>             <artifactid>spring-aop</artifactid>             <groupId>org.springframework</groupId>         </exclusion>    </exclusions></dependency>

Xml

<servlet> <servlet-name>jerseySpring</servlet-name> <servlet-class> Com.sun.jersey.spi.spring.container.servlet.springservlet</servlet-class> <load-on-startup>2</ Load-on-startup></servlet><servlet-mapping> <servlet-name>jerseyspring</servlet-name > <url-pattern>/jersey/*</url-pattern></servlet-mapping>

To facilitate testing, the integrated grizzly,grizzly default is not to load the spring configuration and cannot rely on injected object instances from the spring container, so the following extensions are made.

Custom Myjerseyservlet to load the spring configuration.

Package Com.jersey.demo.web.servlet;import Org.springframework.context.configurableapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import     Com.sun.jersey.spi.spring.container.servlet.springservlet;public class Myjerseyservlet extends SpringServlet {/**    * */private static final long serialversionuid = 1L; @Override protected Configurableapplicationcontext Getdefaultcontext () {return new Classpathxmlapplicationconte    XT ("Applicationcontext.xml"); }}

Start, stop grizzly the main class of the container, there are corresponding adjustments.

package com.jersey.demo;import java.io.ioexception;import  com.jersey.demo.web.servlet.myjerseyservlet;import com.sun.grizzly.http.selectorthread;import  com.sun.jersey.api.container.grizzly.grizzlywebcontainerfactory;public class main {     public static final String URI =  "Http://localhost:8081/myapp";     public static selectorthread startserver ()  throws  Illegalargumentexception,            ioexception  {        //  equivalent to the configuration of Servlet jersey in Web. XML          return grizzlywebcontainerfactory.create (URI,  myjerseyservlet.class,                 null);     }    public static void main (String[] args)  throws IllegalArgumentException,             IOException {         Selectorthread selectorthread = startserver ();         System.out.println ("--------jersey server started!");         system.in.read ();         // stop server        selectorthread.stopendpoint ();         system.out.println ("--------jersey server stoped!");     }}

The test class is the same as in the Jersey integration grizzly.

Jersey integrates spring and uses the grizzly built-in container test jersey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.