我有這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();
}
}
我相信OrderResource需要有一個返回ProductResource的子資源。從'getProducts'中刪除'@ GET' – Tibrogargan
忘記添加路徑參數@PathParam(「orderid」)getProducts中的字符串orderid –
非常感謝您的意見。看看我的編輯。我仍然得到404 –