Introduction
Jakarta EE provides standard HTTP methods, such as GET, POST, PUT, and DELETE, that enable developers to interact with web resources. Understanding these HTTP methods and their proper usage is essential for building web applications. This article will explore how to use different HTTP methods with Jakarta EE and provide examples of their implementation.
Setup
The examples were built inside the following class. The class is annotated with @Path
to set the base path of the class.
The @Produces(MediaType.APPLICATION_JSON)
is placed at the class level because every method returns a JSON object to the client.
As the @Path name does suggest, the methods are about a pet shop. We will implement basic REST calls to keep track of the animals inside our shop. The animals are stored inside a Hashmap.
|
|
@GET
The @GET annotation is used to retrieve information from the REST resource. When a client sends a GET request to a URL mapped to a resource method annotated with @GET, the Jakarta EE server invokes that method and returns the response to the client.
In the following example, the @GET annotated method returns a response object.
|
|
@OPTIONS
Use the @OPTIONS to tell the client which HTTP methods your resource support.
|
|
@POST
The POST method is used to send data to the application. In this case, we use it to create a new pet.
When creating the response, we also build a URI that points to the newly created pet. When a client creates a Pet, the response will have a header called location with the URL as a value. If you create a pet called Jason, the URL would be: http://localhost:8080/my-app/Jason.
|
|
@DELETE
Next up is the @DELETE annotation to delete a pet from the hop. The DELETE HTTP verb is used to delete a resource from the server, and the server responds with a 204 No Content status code to indicate the request was successful.
In the example, we check if there is a pet with the given name. If the pet exists, we delete it from the map and return a 204 status code. If the pet does not exist, a 404 not found is returned to the client.
|
|
@HEAD
The HEAD request is often used to check if a resource exists. In the following example, we return a “204 no content” to tell the client that the pet exists. If the pet doesn't exist, a 404 not found is returned.
|
|
@PUT
@PUT is used to update an existing resource if it exists. In the following example, we update the age and type with the new values the client sent. Lke with the @POST, we again sent a location back to the client of where it can find the pet in the future.
|
|
Conclusion
Jakarta provides a wide range of HTTP verbs that can be used to interact with RESTful web services. These verbs include GET, POST, PUT, DELETE, HEAD, and OPTIONS, each of which has a specific purpose and functionality.