使用jersey在URL中进行REST版本控制

时间:2015-07-28 20:46:21

标签: java spring rest jersey

我有一个Jersey / Spring REST servlet。我正在尝试使用URL版本控制机制来拥有相同资源的2个版本。 解决这个问题的最佳方法是什么?

这是我的web.xml我试图加载2个球衣servlet

<servlet>
    <servlet-name>REST_V1</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.comp.resource.v1</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>REST_V1</servlet-name>
    <url-pattern>/v1/*</url-pattern>
</servlet-mapping>

这是V2映射

<servlet>
    <servlet-name>REST_V2</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.comp.resource.v2</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>REST_V2</servlet-name>
    <url-pattern>/v2/*</url-pattern>
</servlet-mapping>

我已经定义了2个spring组件,在各自的包中具有相同的资源路径

package com.comp.resource.v1;

@Controller
@Path("/user")
public class User_V1 {

}

对于V2

package com.comp.resource.v2;

@Controller
@Path("/user")
public class User_V2 {

}

我发现资源/用户存在冲突的URI模板错误 有没有更好的方法来解决这个问题?任何帮助都值得赞赏

1 个答案:

答案 0 :(得分:1)

Seems like the issue is with how spring beans are loaded. In the web.xml if you have contextConfigLocation outside of the Jersey to load up all the beans, then both REST_V1 and REST_V2 servlet conflicts with the same resource name.

Here is what I changed in the application context. Removed scanning of resource package from global applicationContext.xml

<context:annotation-config />
<context:component-scan base-package="com.comp.*">
    <context:exclude-filter type="regex" expression="com.comp.resource.*"/>
</context:component-scan>

Added 2 more applicationContext, for each servlet

applicationContext_V1.xml

<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v1"/>

applicationContext_V2.xml

<context:annotation-config />
<context:component-scan base-package="com.comp.resource.v2"/>

Added reference to these applicationContext file in jersey configuration in web.xml

<servlet>
    <servlet-name>REST_V1</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.comp.resource.v1</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_V1.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>REST_V1</servlet-name>
    <url-pattern>/v1/*</url-pattern>
</servlet-mapping>

and for REST_V2

<servlet>
    <servlet-name>REST_V2</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.comp.resource.v2</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_V2.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>REST_V2</servlet-name>
    <url-pattern>/v2/*</url-pattern>
</servlet-mapping>