我有这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 –