簡體   English   中英

如何在JAX-RS中返回http創建的資源的位置以及響應狀態消息?

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

我正在研究一種如下的Web服務: 在此處輸入圖片說明

  1. 客戶端使用一些參數將POST請求發送到REST服務器。

  2. REST服務器處理該請求,將數據插入表中,並創建一個具有ID的新行。

  3. 然后,REST服務器向FileServer發送一個關於uploadlink的請求,然后FileServer將uploadLink返回給REST服務器。

  4. 最后,REST服務器將步驟2中ID為ID的新創建資源的位置返回給客戶端。

這是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請求時,得到以下響應: 在此處輸入圖片說明

我的問題是,如何在響應中包含uploadLink? 我真的很感謝任何建議或意見,這里的java noob。

如何在響應中包含uploadLink

如果您打算在響應有效負載中發送URI,則可以使用以下命令:

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

如果要添加Link標頭,請嘗試以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM