2014-10-09 32 views
1

我有dropwizard.io應用程序,並有一個問題,一個GET請求,我的資源是這樣的:發送404,而不是405至極空WebApplicationException

@Path("/foobar") 
public class FooBarResource 
    (...) 
    @GET 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id){ 
    return Response.status(200).entity(FooBarService.get(id)).build(); 
    } 

    @DELETE 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id){ 
    return Response.status(200).entity(FooBarService.delete(id)).build(); 
    } 

    @PUT 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id, FooBar fooBar){ 
    return Response.status(204).entity(FooBarService.update(id, fooBar)).build(); 
    } 
} 

當我送GET localhost:port/appPath/foobar/我有405代替404.我怎樣才能得到404?當我調試我的應用程序時,我擁有的是javax.ws.rs.WebApplicationException,但它是空的。

+1

還有什麼其他方法可以匹配'GET' for localhost:port/appPath/foobar /'? – 2014-10-09 09:09:31

+0

沒有,在整個應用程序。 – gniadson 2014-10-09 09:12:08

+0

檢查是否可以幫助:http://stackoverflow.com/questions/25040222/restful-services-sending-http-405-instead-of-http-500-error – thepace 2014-10-09 09:14:57

回答

0

你應該使用可選的路徑參數,如描述here

@GET 
@Path("/{id:.*}") 
@UnitOfWork 
public Response getFooBar(@PathParam("id") String id){ 
    try { 
     return Response.status(200).entity(FooBarService.get(Long.parseLong(id))).build(); 
    } catch (NumberFormatException ignore) { 
     return Response.status(404).build(); 
    } 
} 
+0

仍然405,應用程序甚至沒有進入getFooBar方法 – gniadson 2014-10-09 10:30:14

+0

看起來很奇怪。如果你用尾部斜線提供請求,應該使用這種方法。使用\ @GET和\ @UnitOfWork註釋添加另一種方法,但沒有\ @Path,它應該處理直接的GET請求。例如,請參閱[Path docs](http://docs.oracle.com/javaee/6/api/javax/ws/rs/Path.html)。 – ursa 2014-10-09 11:02:05

1

您嘗試GET本地主機:端口/ APPPATH/foobar的/,你會得到405(不允許的方法)。原因是這個資源(= foobar)存在,所以404(資源未找到)是錯誤的。如果您創建getFooBar() - > getFooBar2()的路徑annoation的副本,您應該得到一個200.

如果您希望可以創建一個容器篩選器,如果路徑未找到時會引發異常。這個異常然後可以映射到一個404.