zoukankan      html  css  js  c++  java
  • Jersey(1.19.1)

    JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes. For example,

    public class MyApplication extends Application {
        public Set<Class<?>> getClasses() {
            Set<Class<?>> s = new HashSet<Class<?>>();
            s.add(HelloWorldResource.class);
            return s;
        }
    }

    Alternatively it is possible to reuse one of Jersey's implementations that scans for root resource and provider classes given a classpath or a set of package names. Such classes are automatically added to the set of classes that are returned by getClasses. For example, the following scans for root resource and provider classes in packages "org.foo.rest", "org.bar.rest" and in any sub-packages of those two:

    public class MyApplication extends PackagesResourceConfig {
        public MyApplication() {
            super("org.foo.rest;org.bar.rest");
        }
    }

    There are multiple deployment options for the class that implements Application interface in the Servlet 3.0 container. For simple deployments, no web.xml is needed at all. Instead, an @ApplicationPath annotation can be used to annotate the user defined application class and specify the the base resource URI of all application resources:

    @ApplicationPath("resources")
    public class MyApplication extends PackagesResourceConfig {
        public MyApplication() {
            super("org.foo.rest;org.bar.rest");
        }    
        ...
    }

    Another deployment option is to declare JAX-RS application details in the web.xml. This is usually suitable in case of more complex deployments, e.g. when security model needs to be properly defined or when additional initialization parameters have to be passed to Jersey runtime. JAX-RS 1.1 specifies that a fully qualified name of the class that implements Application may be declared in the <servlet-name> element of the JAX-RS application's web.xml. This is supported in a Web container implementing Servlet 3.0 as follows:

    <web-app>
        <servlet>
            <servlet-name>org.foo.rest.MyApplication</servlet-name>
        </servlet>
        ...
        <servlet-mapping>
            <servlet-name>org.foo.rest.MyApplication</servlet-name>
            <url-pattern>/resources</url-pattern>
        </servlet-mapping>
        ...
    </web-app>

    Note that the <servlet-class> element is omitted from the servlet declaration. This is a correct declaration utilizing the Servlet 3.0 extension mechanism. Also note that <servlet-mapping> is used to define the base resource URI.

    When running in a Servlet 2.x then instead it is necessary to declare the Jersey specific servlet and pass the Application implementation class name as one of the servlet's init-param entries:

    <web-app>
        <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>javax.ws.rs.Application</param-name>
                <param-value>org.foo.rest.MyApplication</param-value>
            </init-param>
            ...
        </servlet>
        ...
    </web-app>

    Alternatively a simpler approach is to let Jersey choose the PackagesResourceConfig implementation automatically by declaring the packages as follows:

    <web-app>
        <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>org.foo.rest;org.bar.rest</param-value>
            </init-param>
            ...
        </servlet>
        ...
    </web-app>

    JAX-RS also provides the ability to obtain a container specific artifact from an Application instance. For example, Jersey supports using Grizzly as follows:

    SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class);

    Jersey also provides Grizzly helper classes to deploy the ServletThread instance at a base URL for in-process deployment.

    The Jersey samples provide many examples of Servlet-based and Grizzly-in-process-based deployments.

  • 相关阅读:
    关于百度地图API的地图坐标转换问题
    SQL Server函数大全
    30条SQL查询优化原则
    .net升级到4.0之后,出现;System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798
    Java回调函数的理解
    .NET 开源GIS项目
    微信公众平台开发入门教程
    GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)
    可视化之lattice、recharts、ggplot资源汇总
    回归小知识
  • 原文地址:https://www.cnblogs.com/huey/p/5397419.html
Copyright © 2011-2022 走看看