我如何 return http 创建资源的位置以及 JAX-RS 中的响应状态消息?

How can I return the location of http created resource along with a response status message in JAX-RS?

我正在开发一个网络服务,其工作方式如下:

  1. 客户端向REST服务器发送POST请求,并带有一些参数。

  2. REST服务器处理请求,插入数据到table,创建一个 带有 ID 的新行。

  3. 然后REST服务器向FileServer发送请求上传链接和 FileServer returns 到 REST 服务器的上传链接。

  4. 最后REST服务器returns新建资源的位置 带有 ID,从步骤 2 到客户端。

这是 POST 处理程序:

@POST
public Response postFilesByCustomerId(AbstractPrincipal user, Attachment attachment) {
    Integer id = new AttachmentService(user).createNew(attachment);
    String uploadLink = FileServer.getUploadLinkForFile(user.getDB(), attachment.getUuid(), attachment.getFileName());
    return Response.created(LinkBuilder.getUriFromResource(this.getClass(), id)).build();
}

当我从客户端发送 POST 请求时,我得到了这个响应:

我的问题是,如何在响应中包含上传链接?我非常感谢任何建议或意见,java 菜鸟。

How can I include the uploadLink in the response?

如果您打算在响应的负载中发送 URI,您可以使用以下内容:

URI uri = LinkBuilder.getUriFromResource(this.getClass(), id);
return Response.created(uri).entity(uploadLink).build();

如果您想添加 Link header,请尝试以下操作:

return Response.created(uri).link(uploadLink, "upload").build();