How To return CREATED HTTP Status Code in Jax-Rs

Use created(URI location) of the javax.ws.rs.core.Response class. Usually when you return the 201 HTTP Status Code, you should return a location header with the location where the new REST resource is available.

import javax.ejb.Stateless;
import j…


This content originally appeared on DEV Community and was authored by Adrian Matei

Use created(URI location) of the javax.ws.rs.core.Response class. Usually when you return the 201 HTTP Status Code, you should return a location header with the location where the new REST resource is available.

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Path("messages")
@Stateless
@Tag(name = "Message")
public class MessageRestResource {

  @Inject private MessageService messageService;

  @POST
  @Consumes(MediaType.TEXT_PLAIN)
  @Operation(summary = "Create message")
  @ApiResponses({
    @ApiResponse(
        responseCode = "201",
        description = "Message successfully created."),
    @ApiResponse(responseCode = "403", description = "Forbidden")
  })
  public Response createMessage(
      @Parameter(description = "Message") String message, @Context UriInfo uriInfo)
      throws JAXBException {
    Message created = messageService.createMessage(message);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    builder.path(created.getUuid().toString());
    return Response.created(builder.build()).build();
  }

In the snippet the UriBuilder method builder.path(created.getUuid().toString()) appends the identifier of the newly created message (uuid) to the absolute path of the request, which was obtained from the uriInfo

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2022-02-12T08:51:25+00:00) How To return CREATED HTTP Status Code in Jax-Rs. Retrieved from https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/

MLA
" » How To return CREATED HTTP Status Code in Jax-Rs." Adrian Matei | Sciencx - Saturday February 12, 2022, https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/
HARVARD
Adrian Matei | Sciencx Saturday February 12, 2022 » How To return CREATED HTTP Status Code in Jax-Rs., viewed ,<https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/>
VANCOUVER
Adrian Matei | Sciencx - » How To return CREATED HTTP Status Code in Jax-Rs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/
CHICAGO
" » How To return CREATED HTTP Status Code in Jax-Rs." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/
IEEE
" » How To return CREATED HTTP Status Code in Jax-Rs." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/. [Accessed: ]
rf:citation
» How To return CREATED HTTP Status Code in Jax-Rs | Adrian Matei | Sciencx | https://www.scien.cx/2022/02/12/how-to-return-created-http-status-code-in-jax-rs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.