简体   繁体   中英

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: 在此处输入图片说明

  1. The client sends a POST request to the REST server with some parameters.

  2. The REST server handles the request, inserts the data to a table, creating a new row with an ID.

  3. The REST server then sends a request to the FileServer for uploadlink and the FileServer returns the uploadLink to the REST server.

  4. Finally, the REST server returns the location of the newly created resource with ID, from step 2, to the client.

This is the POST handler:

@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: 在此处输入图片说明

My question is, How can I include the uploadLink in the response? I'd really appreciate any suggestion or advice, java noob here.

How can I include the uploadLink in the response?

If you intend to send the URI in the payload of the response , you can use the following:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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