Return CREATED HTTP Status Code in Jax-Rs

Use created(URI location) of the 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;

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

  @Inject private MessageService messageService;

  @Operation(summary = "Create message")
        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();
    return Response.created(;

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 the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

routerLink with query params in Angular html template

routerLink with query params in Angular html template code snippet Continue reading