jersey的版本变化

时间:2021-02-24 19:33:02

从1.X到2.X:

1:web.xml加载jersey的servlet容器

jersey1.X使用的是sun的com.sun.jersey.spi.container.servlet.ServletContainer

jersey2.X使用的是glassfish的org.glassfish.jersey.servlet.ServletContainer


2:扫描jersey resource

jersey1.X使用的是sun的com.sun.jersey.config.property.packages

jersey2.X使用的是glassfish的jersey.config.server.provider.packages


3:jersey2.X可以使用servlet3的 @WebServlet扫描jersey resource。不需要特别配置web.xml


例如:

import javax.servlet.annotation.WebInitParam;

import javax.servlet.annotation.WebServlet;

import org.glassfish.jersey.servlet.ServletContainer;

@WebServlet(initParams = @WebInitParam(name = "jersey.config.server.provider.packages", value = "com.example"), urlPatterns = "/webapi/*", loadOnStartup = 1)

public class AirServlet extends ServletContainer {

private static final long serialVersionUID = 1L;

}


4:jersey2.X可以使用@ApplicationPath注解,加载jersey resouce。

例如:

@ApplicationPath("/webapi/*")

public class AirApplication extends Application {

@Override

public Set<Class<?>> getClasses() {

final Set<Class<?>> classes = new HashSet<Class<?>>();

classes.add(MyResource.class);

return classes;

}

}

5:jersey2.X可以使用web.xml加载Application

             <servlet>

<servlet-name>Jersey Web Application</servlet-name>

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>javax.ws.rs.Application</param-name>

<param-value>com.example.RestApplication</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Jersey Web Application</servlet-name>

<url-pattern>/webapi/*</url-pattern>

</servlet-mapping>

web.xml相关设置:

<servlet>
    <servlet-name>rest_test</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.jersey</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest_test</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>