How to return a response with OK HTTP Status Code with Jax-Rs

Use the ok() method of the javax.ws.rs.core.Reponse class to create a ReponseBuilder with a status of 200 (OK),
or the ok(Object entity) to return OK with data

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.*;
import java…


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

Use the ok() method of the javax.ws.rs.core.Reponse class to create a ReponseBuilder with a status of 200 (OK),
or the ok(Object entity) to return OK with data

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

@Path("comparison")
@Stateless
@Tag(name = "Comparison")
public class ComparisonRestResource {

  @Inject private ComparisonService comparisonService;

  @HEAD
  @Operation(
      summary = "Ping HEAD",
      description = "Check availability of the resource. ")
  @ApiResponses(@ApiResponse(responseCode = "200", description = "Service is reachable via HTTP"))
  public Response head() {
    return Response.ok().build();
  }

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  @Operation(
      summary = "Ping GET",
      description = "Check availability of the example resource. ")
  @ApiResponses(@ApiResponse(responseCode = "200", description = "Service is reachable via HTTP"))
  public Response ping() {
    return Response.ok("pong").build();
  }
}

Note that the ok() methods shown before are just shortcuts for

return Response
        .status(Response.Status.OK)
        .build()

and

return Response
        .status(Response.Status.OK)
        .entity("pong")
        .build()

respectively.

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-11T15:19:50+00:00) How to return a response with OK HTTP Status Code with Jax-Rs. Retrieved from https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/

MLA
" » How to return a response with OK HTTP Status Code with Jax-Rs." Adrian Matei | Sciencx - Friday February 11, 2022, https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/
HARVARD
Adrian Matei | Sciencx Friday February 11, 2022 » How to return a response with OK HTTP Status Code with Jax-Rs., viewed ,<https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/>
VANCOUVER
Adrian Matei | Sciencx - » How to return a response with OK HTTP Status Code with Jax-Rs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/
CHICAGO
" » How to return a response with OK HTTP Status Code with Jax-Rs." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/
IEEE
" » How to return a response with OK HTTP Status Code with Jax-Rs." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-jax-rs/. [Accessed: ]
rf:citation
» How to return a response with OK HTTP Status Code with Jax-Rs | Adrian Matei | Sciencx | https://www.scien.cx/2022/02/11/how-to-return-a-response-with-ok-http-status-code-with-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.