2017-04-23 41 views
0

我有這2個資源錯誤404訪問的子資源在JAX-RS新澤西

@Path("/orders") 
public class OrderResource { 

    @GET 
    @Path("/{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getOrder(@PathParam("id") String orderid) 
      throws JSONException { 
     Order order = db.getOrder(orderid); 
     return Response.status(Status.OK).entity(order).build(); 
    } 

    @GET 
    @Path("/{orderid}/products") 
    public ProductResource getProducts() { 
     return new ProductResource(); 
    } 

} 

@Path("/") 
public class ProductResource { 

    @GET 
    @Path("/{productid}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getProduct(@PathParam("orderid") String orderid, @PathParam("productid") String productid) throws JSONException { 
     Product product = db.getProduct(productid); 
     return Response.status(Status.OK).entity(product).build(); 
    } 
} 

我得到一個成功的輸出當我這樣做:

http://localhost:8080/testApp/api/orders/O101 

我能看到的集合鏈接到輸出的順序的產品,所以我複製的ID和嘗試這個

http://localhost:8080/testApp/api/orders/O101/products/P101 

但我總是得到一個404錯誤。爲什麼?我該如何解決這個問題?

這是我在web.xml

<servlet-mapping> 
    <servlet-name>TestApp</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
    </servlet-mapping> 

編輯配置

非常感謝你對你的答案。今天早上醒來累了,沒有成功測試它。

我想你的建議,但仍獲得404

@Path("/orders") 
public class OrderResource { 

    @GET 
    @Path("/{id}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getOrder(@PathParam("id") String orderid) 
      throws JSONException { 
     Order order = db.getOrder(orderid); 
     return Response.status(Status.OK).entity(order).build(); 
    } 

    @GET 
    @Path("/{orderid}/products") //Here I added after products /{productID} which gives me an empty JSON. Never reach the method from the subresource. 
    public ProductResource getProducts() { 
     return new ProductResource(); 
    } 

} 

public class ProductResource { 

    @Path("/{productid}") //Here I tried to remove the slash also. 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response getProduct(@PathParam("orderid") String orderid, @PathParam("productid") String productid) throws JSONException { 
     Product product = db.getProduct(productid); 
     return Response.status(Status.OK).entity(product).build(); 
    } 
} 
+1

我相信OrderResource需要有一個返回ProductResource的子資源。從'getProducts'中刪除'@ GET' – Tibrogargan

+1

忘記添加路徑參數@PathParam(「orderid」)getProducts中的字符串orderid –

+0

非常感謝您的意見。看看我的編輯。我仍然得到404 –

回答

0

的問題是在getProducts@GET。子資源定位符被定義爲具有@Path並且具有@METHOD的方法。如果你仔細想一想,這是非常有意義的,因爲在子資源類中可以不只是說一個@GET。所以刪除@GET,它應該工作。離開它會導致方法不是一個子資源定位器,它會像一個正常的資源方法。

除此之外,別人有關於@Path("/")的建議是不是問題的原因,但它是一個問題。這樣做會導致澤西島也將ProductsResource註冊爲根資源。所以將能夠訪問/api/1234,因爲它映射到/。你可能不想要這個。所以你應該從ProductsResource中刪除@Path("/")

+0

非常感謝您的寶貴答案。看看我的編輯。我仍然得到了404。 –

+0

1.您沒有從'getProducts()'中刪除'@ GET'。 2.您需要將'@ GET'添加回子資源類 –

+0

中的方法謝謝。我誤解了。有用 –

0

子資源不應該與@Path上一流水平進行註釋,他們需要與JAX-RS runtinme註冊。

只需刪除@Path註釋。

+0

非常感謝你的寶貴答案。看看我的編輯。我仍然得到404。 –

0

就你而言,問題似乎是你的子資源中的註解@Path。定義一個子資源時,不應該在@Path級別註釋它。同樣在您的ProductResource中,嘗試從@Path(「/ {productid}」)中刪除'/',因爲它應該從父級的上下文(OrderResource)中引用,並且不應作爲單個實例存在。

感謝

+0

非常感謝您提供寶貴的答案。看看我的編輯。我仍然拿到404. –

+1

哦,我明白了,讓我試試一個示例代碼,如果成功將與您分享:-) –