简体   繁体   English

REST如何在URI中传递空路径参数?

[英]REST how to pass empty path parameter in the URI?

I am developing a restful webapp. 我正在开发一个安静的webapp。 In this the parameters i take are userid and orderid. 在这里,我采用的参数是userid和orderid。

The userid can be null . userid可以为null
The URI is @Path("api/user/userid/order/orderid") My method is, URI是@Path("api/user/userid/order/orderid")我的方法是,

void add(@PathParam("userid") String userId, @PathParam("orderid") String orderId);

I want to pass null value for userId in the URI. 我想在URI中为userId传递null值。

I tried api/user//order/1234 . 我试过api/user//order/1234 But in this case, the userid takes the value of orderId (ie 1234 and orderId is null . (which is wrong) 但在这种情况下, userid获取orderId的值(即1234, orderIdnull 。(这是错误的)

I also tried changing the path as @Path("api/user/userid: .*/order/orderid") . 我也尝试将路径更改为@Path("api/user/userid: .*/order/orderid") Yet the same result as previous. 然而与之前的结果相同。

The other ways for solving this could be, using @QueryParam for userid or creating another method for userid null . 解决这个问题的其他方法可能是,使用@QueryParam进行userid或为userid null创建另一种方法。

But I would like to know if there is a way to have userId as PathParameter instead of QueryParam and pass the value of userid as null ? 但我想知道是否有办法将userId作为PathParameter而不是QueryParam并将userid的值传递为null

Having a URL param as null is not a valid case. 将URL参数设置为null不是有效的情况。

POSTing to the URL api/user/userid/order/orderid is saying "create an order for this user". 发布到URL api/user/userid/order/orderid是说“为该用户创建订单”。 GETting to that same URL would be saying "retrieve me this specific order for this user". GET到同一个URL会说“检索我这个用户的特定订单”。 Both are in the context of a user. 两者都在用户的上下文中。

For doing order creation or retrieval outside of the context of a user, just remove those two parts of your URL... 要在用户的上下文之外进行订单创建或检索,只需删除URL的这两部分...

api/order/orderid

This would allow you to create and get orders outside of the bounds of a customer. 这将允许您创建和获取客户范围之外的订单。 This would also work for in the context of a user. 这也适用于用户的上下文。 If you POST an order with no userID, nobody cares, but one can be specified. 如果您在没有用户ID的情况下发布订单,则无人问津,但可以指定一个。 And if you GET an order, it shouldn't care which customer it is for, unless your orderID isn't unique across customers but that is probably not a good idea but that is a separate discussion. 如果您获得订单,它不应该关心它是哪个客户,除非您的订单ID在客户中不是唯一的,但这可能不是一个好主意,但这是一个单独的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM