When to use @QueryParam vs @PathParam

In a Java RESTful web service, the @QueryParam annotation is used to bind a query parameter to a method parameter, while the @PathParam annotation is used to bind a path parameter to a method parameter.

A query parameter is a value that is included in the query string of a URL. Query parameters are added to the end of the URL after a ? character, and multiple query parameters are separated by & characters. For example, in the URL https://example.com/search?q=java&page=1, the q and page parameters are query parameters.

A path parameter is a placeholder in a URL path that is used to match a value in the request. Path parameters are typically used to identify a specific resource in the URL. For example, in the URL https://example.com/users/123, the 123 parameter is a path parameter.

Here's an example of how to use @QueryParam and @PathParam in a Java RESTful web service:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

public class UserResource {

  public String getUser(@PathParam("id") int id) {
    // code to retrieve user with id

  public String searchUsers(@QueryParam("q") String query, @QueryParam("page") int page) {
    // code to search for users with query and paginate results

In this example, the getUser method has a @PathParam annotation that binds the id path parameter to the id method parameter. The searchUsers method has @QueryParam annotations that bind the q and page query parameters to the query and page method parameters, respectively.

I hope this helps! Let me know if you have any questions.