2012-04-19 95 views
2

我最近遇到了一種情況,我希望在單個Spring MVC Web應用程序中使用兩個單獨的Jersey應用程序。我創建了兩個映射到不同URI的獨立servlet,以及兩個具有相同路徑的獨立根資源類。多個Jersey應用程序具有相同的根資源路徑

應用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 ,我期待這個工作。這在澤西島是不可能的,還是有不同的方法?

+0

你的應用程序類是怎麼樣的?我的意思是public.api.rest.Application1和public.api.rest.Application2。你可以發佈該代碼嗎? – 2012-04-20 22:26:15

+0

剛剛編輯帖子,添加App1Root和App2Root的代碼 – SkP 2012-04-26 14:36:15

回答

0

我看到多個問題:

  1. 不要使用相同的類的應用程序和資源。資源按每個請求進行實例化。這意味着構造函數應該是輕量級的。您不希望爲每個請求實例化PackagesResourceConfig。即爲應用程序使用單獨的類,爲資源使用單獨的類。
  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 
} 
+0

不幸的是,這並沒有幫助。我編輯了我的帖子以顯示最新的代碼。 – SkP 2012-05-01 15:36:11

+0

你在我的回覆中看到子彈2)嗎?同樣,您正在使用包掃描功能,該功能可查找給定包中的所有資源。如果您想要在應用程序中使用該資源並擁有不同的資源,則必須爲這些資源使用不同的軟件包。 – 2012-05-02 09:23:57

+0

是的。儘管它們有兩個獨立的軟件包:「private.api.rest」和「public.api.rest」 - 這不應該足夠嗎? – SkP 2012-05-02 14:29:25

相關問題