2013-10-31 67 views
3

我面臨一個奇怪的問題,我的澤西島休息Servlet,我使用澤西島1.17.1與配置波紋管,但是,當我嘗試訪問我的休息服務,它只給我一個404 - 未找到錯誤。澤西島給404-沒有發現有效的服務

奇怪的是該日誌:接受根資源類: 「/」,但我不要求對「/」我請求在「/ R/...」

com.sun.jersey.config.property.resourceConfigClass

和日誌表明服務是否正確裝入:

Root resource classes found: 
    class com.xxx.restful.SearchRESTService 

在這裏你可以看到正在運行的服務:

2 < X-Jersey-Trace-001: match path "/" -> "/application\.wadl(/.*)?", "/request(/.*)?", "/search(/.*)?" 

的web.xml

<context-param> 
    <param-name>context-root</param-name> 
    <param-value>root</param-value> 
</context-param> 

<servlet> 
     <servlet-name>JerseyREST</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name> 
      <param-value>com.xxx.restful.ResourceConfig</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 

     <init-param> 
      <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> 
      <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name> 
      <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.config.feature.Trace</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.config.feature.Debug</param-name> 
      <param-value>true</param-value> 
     </init-param> 

     <load-on-startup>3</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>JerseyREST</servlet-name> 
     <url-pattern>/r/*</url-pattern> 
    </servlet-mapping> 

休息服務:

@Path("/search") 
public class SearchRESTService { 

    @GET 
    @Path("/related") 
    @Produces({ MediaType.APPLICATION_JSON }) 
    public Response getRelatedSearch(@QueryParam("query") String query) { 
     List<String> result = ...; 
     return Response.ok(result).build(); 
    } 
} 

啓動日誌

INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM' Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.PackagesResourceConfig init 
INFO: Scanning for root resource and provider classes in the packages: 
    com.xxx.restful 
Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Root resource classes found: 
    class com.xxx.restful.SearchRESTService 
Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Provider classes found: 
    class com.xxx.restful.NucleusProducer 
Oct 31, 2013 1:37:52 PM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder <init> 
INFO: Instantiated the Application class com.xxx.restful.NucleusResourceConfig 

請求日誌

Oct 31, 2013 1:38:08 PM com.sun.jersey.api.container.filter.LoggingFilter filter 
INFO: 2 * Server in-bound request 
2 > OPTIONS http://localhost:7103/root/r/search/related?query=arduino 
2 > Host: localhost:7103 
2 > Connection: keep-alive 
2 > User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 
2 > Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo 
2 > Content-Type: application/json 
2 > Accept: */* 
2 > DNT: 1 
2 > Accept-Encoding: gzip,deflate,sdch 
2 > Accept-Language: en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4 
2 > 

Oct 31, 2013 1:38:08 PM com.sun.jersey.api.container.filter.LoggingFilter$Adapter finish 
INFO: 2 * Server out-bound response 
2 < 404 
2 < X-Jersey-Trace-000: accept root resource classes: "/" 
2 < X-Jersey-Trace-001: match path "/" -> "/application\.wadl(/.*)?", "/request(/.*)?", "/search(/.*)?" 
2 < X-Jersey-Trace-002: mapped exception to response: [email protected] -> 404 (Not Found) 
2 < 
+0

您的服務器是否支持'context-root'上下文參數?如果你不在URL中使用'root',它會說什麼? –

回答

1

我認爲你需要添加下面幾行你web.xml。假設你的球衣類是在包com.example,那麼你就需要添加:

<init-param> 
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>com.example</param-value> 
</init-param> 

這將確保你的根資源類可以通過新澤西州被發現。

+0

這不是問題,導致服務使用com.sun.jersey.config.property.resourceConfigClass屬性加載。並且該日誌顯示該服務已正確加載:找到的根資源類: class com.xxx.restful.SearchRESTService 在此處可以看到該服務正在運行:2 」/application\.wadl(/.*)?「,」/ request(/.*)?「,」/search(/.*)?「 –

+0

如果將'/ r/*'更改爲'/ *'會怎樣?你看到相同的404錯誤嗎? – tonga

+0

是的,同樣的錯誤。我將服務從/搜索更改爲/,並且它們給出了403,而非賦予的方法。 –