简体   繁体   English

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

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

I'm working on a web service that works as follows: 我正在研究一种如下的Web服务: 在此处输入图片说明

  1. The client sends a POST request to the REST server with some parameters. 客户端使用一些参数将POST请求发送到REST服务器。

  2. The REST server handles the request, inserts the data to a table, creating a new row with an ID. REST服务器处理该请求,将数据插入表中,并创建一个具有ID的新行。

  3. The REST server then sends a request to the FileServer for uploadlink and the FileServer returns the uploadLink to the REST server. 然后,REST服务器向FileServer发送一个关于uploadlink的请求,然后FileServer将uploadLink返回给REST服务器。

  4. Finally, the REST server returns the location of the newly created resource with ID, from step 2, to the client. 最后,REST服务器将步骤2中ID为ID的新创建资源的位置返回给客户端。

This is the POST handler: 这是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();
}

When I send POST request from the client, I get this response: 当我从客户端发送POST请求时,得到以下响应: 在此处输入图片说明

My question is, How can I include the uploadLink in the response? 我的问题是,如何在响应中包含uploadLink? I'd really appreciate any suggestion or advice, java noob here. 我真的很感谢任何建议或意见,这里的java noob。

How can I include the uploadLink in the response? 如何在响应中包含uploadLink

If you intend to send the URI in the payload of the response , you can use the following: 如果您打算在响应有效负载中发送URI,则可以使用以下命令:

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

If you want to add a Link header, try the following: 如果要添加Link标头,请尝试以下操作:

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

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

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