2012-09-07 64 views
2

起初在我的web服務器上,我只有一個REST servlet。喜歡的東西:Jersey找不到REST webservice子資源

@Path("/") 
public class Controller { 
    @GET 
    @Produces({ MediaType.TEXT_HTML }) 
    public Response get(@Context UriInfo info) throws Exception { 
    ... 
    } 

    @GET 
    @Path("resource1") 
    @Produces({ MediaType.TEXT_HTML }) 
    public Response resource1() throws Exception { 
    ... 
    } 

    ... 

} 

而且在web.xml:

<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>xpto.mypack1;xpto.mypack2</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>rest</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

但後來我想了一些靜態html添加到服務器,所以我更新了servlet映射到 /REST/*

和控制器servlet類的@Path指令從「/」到「/ rest」。一切工作正常,但子資源或控制器的方法與@Path指令,停止工作..即:

  • /正常工作,因爲我有一個index.html頁在根
  • /休息工作正常,它調用我的servlet
  • /REST /資源1返回404 HTTP代碼的get方法...

任何幫助嗎?我已經嘗試過/後各@Path指令之前的組合的列表,沒有成功...非常感謝

一個更新:

我使用的跟蹤UTIL,得到了以下結果:

爲/ [應用名稱] /休息(它的工作原理):

  • X-新澤西州痕量-002接受右手路徑java.util.regex.Matcher中[圖案= /休息(/.*)? X-Jersey-Trace-003接受資源:「休息」 - > @路徑(「/休息」):「/ rest」 - >「/休息」:「」
  • xpto.mypack.Controller
  • X-新澤西州跟蹤的000根接受資源類: 「/休息」
  • X-新澤西州跟蹤的001匹配路徑 「/休息」 - >「/application.wadl(/。 )?「,」/ rest(/。)?「

用於/ [應用程序名]/REST /資源1(它不工作):

  • X-新澤西州跟蹤的002匹配的異常映射:com.sun.jersey.api。 NotFoundException @ 4fd41dc3 - > xpto.myclass
  • X-新澤西州痕量-003映射例外響應:[email protected] - > 404(未找到) X-新澤西州痕量-000接受根資源類:「/ resource1」 X-Jersey-Trace-001匹配路徑「/ resource1」 - >「/application.wadl(/。)?」,「/ rest(/。)?」

我希望它可以幫助別人幫我..

+0

你有沒有試過設置路徑到'(「/ resource1」)' – ant

+0

是的,我試過了,結果相同。我試圖調試以弄清楚發生了什麼,並且我發現與模式相比較的url部分只是「休息」之後的部分。以休息結束的部分被認爲是「基礎網址」。這讓我很困惑。順便說一句,我使用的是tomcat。 :( – cattox

+0

現在我無法理解這一切......我從控制器@Path刪除斜線,使其@Path(「」),一切工作正常......任何人都可以解釋我爲什麼?:S – cattox

回答

4

如果你定義你的servlet映射,/rest/*,在@Path註釋你的資源,不重複/rest。即所有你需要做的就是保持控制器原樣(在你的問題上面),只需改變servlet映射。在該資源可用的網址是:

<application_context_path>/<servlet_mapping> 

因此,如果您更改@Path("/")@Path註釋@Path("rest"),你也改變servlet映射到/rest,那麼你的資源將可在:

<application_context_path>/rest/rest/* 
+0

做你的意思是讓「/ rest」作爲servlet_mapping和@Path(「/」)?或者「/ rest/*」和@Path(「/」)? – cattox