【问题标题】:Multiple Jersey Applications with same path for root resource具有相同根资源路径的多个 Jersey 应用程序
【发布时间】:2012-04-19 15:48:36
【问题描述】:

我最近遇到了一种情况,我希望在一个 Spring MVC Web 应用程序中包含两个独立的 Jersey 应用程序。我创建了两个映射到不同 URI 的独立 servlet,以及两个具有相同路径的独立根资源类。

应用程序 1 Servlet:

<servlet>
    <servlet-name>JerseyServlet1</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>public.api.rest.Application1</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>JerseyServlet1</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

应用程序 2 Servlet:

<servlet>
    <servlet-name>JerseyServlet2</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>private.api.rest.Application2</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>JerseyServlet2</servlet-name>
    <url-pattern>/internal/*</url-pattern>
</servlet-mapping>

应用1:

package public.api.rest;

public class Application1 extends PackagesResourceConfig {
    public Application1(){
        super(Application1.class.getPackage().getName());
    }
}

应用 2:

package private.api.rest;

public class Application2 extends PackagesResourceConfig {
    public Application2(){
        super(Application2.class.getPackage().getName());
    }
}

应用程序 1 根资源:

package public.api.rest;

@Path("release-1")
@Component
@Scope("request")
public class App1Root{
    //resource methods
}

应用程序 2 根资源:

package private.api.rest;

@Path("release-1")
@Component
@Scope("request")
public class App2Root{
    //resource methods
}

应用程序初始化期间抛出的泽西异常:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Conflicting URI templates. The URI template /release-1 for root resource class private.api.rest.App2Root and the URI template /release-1 transform to the same regular expression /release-1(/.*)?

由于它们是两个独立的应用程序和两个独立的 servlet,我期待它能够正常工作。这在泽西岛是不可能的还是有不同的方法?

【问题讨论】:

  • 您的应用程序类是什么样的?我的意思是 public.api.rest.Application1 和 public.api.rest.Application2。你能发布那个代码吗?
  • 刚刚编辑帖子添加了App1Root和App2Root的代码

标签: spring jersey jax-rs


【解决方案1】:

我发现多个问题:

  1. 不要对应用程序和资源使用相同的类。资源是按请求实例化的。这意味着构造函数应该是轻量级的。您不想为每个请求实例化 PackagesResourceConfig。 IE。对应用程序使用单独的类,对资源使用单独的类。
  2. 您的所有代码都位于一个包中,并且您正在使用包扫描。这意味着,当 App1Root 被 servlet 实例化时,它会扫描 private.api.rest 包(以及所有子包)的包(因为您继承自 PackagesResourceConfig),发现 App1Root 和 App2Root 都使用相同的 @Path 注释失败了。

您可以通过拆分资源和应用并将 app1 的资源放入与 app2 不同的包中来解决此问题。例如。以下应该有效:

应用程序1:

package private.api.rest;

public class App1Root  extends PackagesResourceConfig {
    public App1Root(){
        super("private.api.rest.app1");
    }
}

应用程序2:

package private.api.rest;

public class App2Root  extends PackagesResourceConfig {
    public App2Root(){
        super("private.api.rest.app2");
    }
}

资源类 1:

package public.api.rest.app1;

@Path("release-1")
@Component
@Scope("request")
public class App1Root {
    // resource methods here
}

资源类 2:

package public.api.rest.app2;

@Path("release-1")
@Component
@Scope("request")
public class App1Root {
    // resource methods here
}

【讨论】:

  • 不幸的是,这没有帮助。我已经编辑了我的帖子以显示最新的代码。
  • 您在我的回复中看到项目符号 2) 了吗?同样,您正在使用包扫描功能,该功能可查找给定包中的所有资源。如果您想使用它并在您的应用程序中拥有不同的资源,您必须为这些资源使用不同的包。
  • 是的。它们位于两个单独的包中:“private.api.rest”和“public.api.rest”——这还不够吗?
  • 啊,对不起,我错过了。我尝试了您的示例 - 使用了不同的包名称,因为 public.... 和 private... 无效。为我工作,但没有尝试过 SpringServlet - 只是普通的。无论如何,在进行包装更改后,您是否清理过您的项目?如果你不清理,旧的类文件仍然存在,这将解释错误。确保你做干净的构建。
  • 你是对的。当它不是 SpringServlet 时它可以工作。有没有办法让它与 SpringServlet 一起工作?我们想使用请求级别的 Spring Security 进行授权
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 2022-11-04
  • 2014-04-08
  • 2018-01-28
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
相关资源
最近更新 更多