What is the difference between @PathParam and @QueryParam

RestJerseyJax Rs

Rest Problem Overview


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

Rest Solutions


Solution 1 - Rest

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.com/tom?id=1

Solution 2 - Rest

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 - Rest

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.

Solution 4 - Rest

    @javax.ws.rs.QueryParam
    This annotation allows you to extract values from URI query parameters.
    @javax.ws.rs.PathParam
    This annotation allows you to extract values from URI template parameters.
       
        PART-1 : @javax.ws.rs.PathParam
       
        @Path("/mercedes")
        public class MercedesService {
        @GET
        @Path("/e55/{year}")
        @Produces("image/jpeg")
        public Jpeg getE55Picture(@PathParam("year") String year) {
        ...
        }
    
    If I query the JAX-RS service with GET /mercedes/e55/2006, the getE55Picture()
    method would match the incoming request and would be invoked.
    
    PART-2 : @javax.ws.rs.QueryParam
   
 URI might look like this: GET /cus?start=0&size=10
    
        @Path("/cus")
        public class GreedCorruption {
        @GET
        @Produces("application/xml")
        public String getDeathReport(@QueryParam("start") int start,
        @QueryParam("size") int size) {
        ...
        }
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMellonView Question on Stackoverflow
Solution 1 - RestRubenView Answer on Stackoverflow
Solution 2 - RestJRishiView Answer on Stackoverflow
Solution 3 - RestHien NguyenView Answer on Stackoverflow
Solution 4 - Restnatwar kumarView Answer on Stackoverflow