Java 访问JAX-RS服务器中的子资源时出现错误404
我有这两种资源Java 访问JAX-RS服务器中的子资源时出现错误404,java,rest,jersey,jax-rs,Java,Rest,Jersey,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.get
@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>
子资源不应该在类级别用
@Path
注释,它们需要在JAX-RS runtinme中注册
只需删除
@Path
注释。在您的情况下,问题似乎是子资源中的注释@Path。定义子资源时,不应在类级别使用@Path对其进行注释。同样在ProductResource中,尝试从@Path(“/{productid}”)中删除“/”,因为它应该从父级(OrderResource)的上下文中引用,并且不应该作为单个实例存在
谢谢问题在于
getProducts
上的@GET
。子资源定位器定义为具有@Path
且没有@method
的方法。如果您仔细考虑一下,它是非常有意义的,因为在子资源类中可能不仅仅是一个@GET
。因此,删除@GET
,它应该可以工作。保留它将导致该方法不是子资源定位器,并且它的行为将类似于普通资源方法
除此之外,其他人对
@Path(“/”
的建议不是问题的原因,而是一个问题。这会导致Jersey也将ProductsResource
注册为根资源。因此可以访问/api/1234
,因为它映射到/
。你可能不想要这个。因此,您应该从产品资源中删除@Path(“/”
)我认为OrderResource需要一个子资源来返回ProductResource。从getProducts
中删除@GET
,您会忘记在getProducts中添加path param@PathParam(“orderid”)字符串orderid谢谢您的评论。看看我的编辑。我仍然得到了答案,非常感谢你宝贵的回答。看看我的编辑。我仍然得到404.1。您没有从getProducts()
中删除@GET
。2.您需要将@GET
添加回子资源类中的方法非常感谢您宝贵的回答。看看我的编辑。我仍然得到404。哦,我明白了,让我试试一个示例代码,如果成功,我将与您分享:-)非常感谢您宝贵的答案。看看我的编辑。我仍然得到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();
}
}