Define request URL in Jakarta EE RESTful Web Services application

In the Jakarta EE RESTful Web Services application, to define the request URL, we will use the @Path annotation. This annotation has only one attribute, value(), whose value is the request URL we want to define.

You can define the @Path annotation at class level or method level.

The definition at the class level is mandatory!

At the class level, the value defined for this annotation will be the prefix for all requests defined inside the class.

For example, I have an application that defines the request URL as follows:

then you can request to this application with the request URL as follows:

If you don’t want to define prefix for URL requests inside the class, you can define the @Path annotation with the value @Path(“/”) or @Path(“”)!

Defining the @Path annotation at the method level helps us specify which URL request is handled by which method.

To define the path parameter for URL requests, we will define a pair of “{“, “}” with the parameter name in the middle of this pair in the @Path annotation.

The value of the parameter will be bound to the request URL using the @PathParam annotation, for example as follows:

In the above example, I have defined a path parameter named name.

Result:

To define request parameters for request URLs, we will use the @QueryParam annotation.

Example is as follows:

For this example, I have defined a query parameter named name.

Result:

Jakarta EE RESTful Web Service also supports us to define request URL with matrix param, using @MatrixParam annotation.

Matrix param makes it possible to define the value of a more complex parameter, such as a collection.

These matrix parameters are separated by semicolons.

For example, I define the request URL as follows:

If now, I request to the URL http://localhost:8080/hello;name=khanh,java;address=A,B, you will see the following result:

Add Comment