📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Overview
- @PathParam
- @DefaultValue
- @QueryParam
- @FormParam
- @MatrixParam
- @CookieParam
- @HeaderParam
2. The @PathParam Annotation
@PathParam
to extract a path parameter from the path component of the request URL that matched the path declared in @Path. Example: @Path("{userid}/")
public String getUser(@PathParam("userid") String userid) {
// return user object as json
}
@Path("/users/{username}")
public class UserResource {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
...
}
}
3. The @QueryParam Annotation
?
symbol. So for example, in the URL: http://localhost:8080/api/books/search?keyword=Java&limit=10 the query parameters are keyword and limit and the query values are Java and 10.@QueryParam
annotation and pass the name of the query parameter as a value to the annotation, then annotated a method parameter in the resource method that responds to a request to the URI resource /books/search.@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("search")
public Response searchBook(@QueryParam("keyword") String keyword, @QueryParam("limit") int limit) {
List<Book> books = bookRepository.searchBook(keyword, limit);
return Response.ok(new GenericEntity<List<Book>>(books) {}).build();
}
4. The @FormParam Annotation
@FormParam
is slightly special because it extracts information from a request representation that is of the MIME media type "application/x-www-form-urlencoded" and conforms to the encoding specified by HTML forms, as described here. This parameter is very useful for extracting information that is POSTed by HTML forms, for example, the following extracts the form parameter named "name" from the POSTed form data. Example: Processing POSTed HTML form@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("name") String name) {
// Store the message
}
@FormParam
annotation.@POST
@Produces(MediaType.APPLICATION_JSON)
public Response saveBookF(@FormParam("title") String title,
@FormParam("author") String author,
@FormParam("price") Float price) {
return Response.ok(bookRepository.saveBook(new Book(title, author, price))).build();
}
5. The @MatrixParam Annotation
http://localhost:8080/api/books;author=atheedom;category=Java;language=english
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getBookBy(@MatrixParam("author") String author,
@MatrixParam("category") String category,
@MatrixParam("language") String language) {
return Response.ok(
new GenericEntity<List<Book>>(
bookRepository.getBookBy(author, category, language)) {}).build();
}
6. The @CookieParam Annotation
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCart(@CookieParam("cartId") int cartId) {
return Response.ok().build();
}
7. The @HeaderParam Annotation
@HeaderParam
annotation is used to inject HTTP request header values into resource method parameters. You can think of it like a shortcut to using the @Context
annotation to inject theHttpServletRequest or HttpHeaders
instance.@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getReferrer(@HeaderParam("referer") String referrer) {
return Response.ok(referrer).build();
}
Comments
Post a Comment
Leave Comment