0

好的,这是服务器端的代码,我只是对如何在客户端定义路径有疑问。这是服务器上的方法

@Path("{index}/{product}/{amount}")
@PUT
@Produces("text/plain")
public String editTable (@PathParam("index") Integer index, @PathParam("product")     String product, @PathParam("amount") Integer amount)
{...}

现在在客户端

{url = new URL( "http://localhost:8080/OrderService/service/tableservice/"+num+"/"+product+"/"+amount);
.....}

/"+num+"/"+product+"/"+amount);

这是正确的语法吗?

数字和数量也可以是整数,而产品是字符串还是我会有问题?

4

2 回答 2

0

如果您将任何映射@QueryParam到整数,请确保您注意以下事项:
从Oracle 文档上的链接:

If a query parameter exists in the query component of the request URI, then the value will be extracted and parsed as a 32–bit signed integer and assigned to the method parameter. If the value cannot be parsed as a 32–bit signed integer, then an HTTP 400 (Client Error) response is returned.

为了安全起见,拥有你的indexand amountas String。在您的服务中将它们解析为 Integer 并处理NumberFormatException而不是 HTTP 400。

于 2013-01-12T13:25:39.543 回答
0

如果产品名称中包含“不安全”的 URL 字符,您将遇到问题。可能会是这样。为了解决这个问题,您可以在将名称附加到 URL 之前对名称进行 URL 编码。

我认为你应该首先重新考虑你的 PATH 定义。一个好的资源端点应该唯一地标识它。但在您的情况下,您amount在 URL 中包含一个字段!这意味着根据谁订购产品,资源 URL 会发生变化:

客户 A 订购了 2 个 Furbies:

/OrderService/service/tableservice/9/Furbies/2

客户 B 订购了 1 个 Furbie

/OrderService/service/tableservice/9/Furbies/1

但在这两种情况下,客户都试图订购相同的东西——一只菲比!这不是 RESTful。相反,您应该定义一个唯一的资源端点,然后将附加的订单信息作为附加数据发送。例如:

public class Order {
    private Integer productId;
    private Integer amount;
}

@PUT
@Path("/{productId}/orders")
@Produces("text/plain")
public String updateOrder(@PathParam("productId"), Order order) { ... }

您会注意到我也删除了索引字段,如果绝对需要,您可以将其重新添加。您会注意到我还在ordersURL 的末尾添加了一个后缀。表示您正在为产品订单添加更新的表示。

于 2013-01-12T13:27:12.440 回答