What is the difference between @PathParam and @QueryParam

64,066

Solution 1

Query parameters are added to the URL after the ? mark, while a path parameter is part of the regular URL.

In the URL below tom could be the value of a path parameter and there is one query parameter with the name id and value 1:

http://mydomain.example/tom?id=1

Solution 2

Along with the above clarification provided by @Ruben, I want to add that you can also refer equivalent of the same in Spring RESTFull implementation.

JAX- RS Specification @PathParam - Binds the value of a URI template parameter or a path segment containing the template parameter to a resource method parameter, resource class field, or resource class bean property.

@Path("/users/{username}")
public class UserResource {

        @GET
        @Produces("text/xml")
        public String getUser(@PathParam("username") String userName) {
            ...
        }
    }

@QueryParam - Binds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property.

URI : users/query?from=100

@Path("/users")
public class UserService {

    @GET
    @Path("/query")
    public Response getUsers(
        @QueryParam("from") int from){
}}

To achieve the same using Spring, you can use

@PathVariable(Spring) == @PathParam(Jersey, JAX-RS),

@RequestParam(Spring) == @QueryParam(Jersey, JAX-RS)

Solution 3

Additionally, query parameter can be null but path parameter can't. If you don't append the path parameter, you will get 404 error. So you can use path parameter if you want to send the data as mandatory.

Share:
64,066

Related videos on Youtube

Mellon
Author by

Mellon

Software Engineer

Updated on August 29, 2020

Comments

  • Mellon
    Mellon about 3 years

    I am newbie in RESTful jersey. I would like to ask what is the different between @PathParam and @QueryParam in jersey?