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

Subscribe to our newsletter for more code resources and news

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being

Default value for jax-rs query parameter

Default value for jax-rs query parameter code snippet Continue reading

Unmarshalling xml string to java object with JAXB

Published on February 20, 2022

Execute delete statement with jpa query

Published on February 19, 2022