【问题标题】:REST versioning in URL using jersey使用球衣在 URL 中进行 REST 版本控制
【发布时间】:2015-07-28 20:46:21
【问题描述】:

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

这是我的 web.xml 我正在尝试加载 2 个 jersey 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 {

}

我看到资源 /user 的 URI 模板冲突错误 有没有更好的方法来解决这个问题?任何帮助将不胜感激

【问题讨论】:

标签: java spring rest jersey


【解决方案1】:

似乎问题在于如何加载弹簧豆。在 web.xml 中,如果您在 Jersey 之外有 contextConfigLocation 来加载所有 bean,则 REST_V1 和 REST_V2 servlet 与相同的资源名称冲突。

这是我在应用程序上下文中所做的更改。 从全局 applicationContext.xml 中删除了对资源包的扫描

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

为每个 servlet 增加了 2 个 applicationContext

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"/>

在 web.xml 的 jersey 配置中添加了对这些 applicationContext 文件的引用

<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>

对于 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 2018-09-26
    • 2015-01-01
    • 1970-01-01
    • 2015-07-01
    • 2011-05-27
    • 2016-03-08
    • 1970-01-01
    相关资源
    最近更新 更多