Skip to main content
basic formatting
Source Link
DB5
  • 14.1k
  • 7
  • 69
  • 73

I am giving one exapmle to undersand when do we use @Queryparam and @pathparam

For example I am taking one resouce is carResource class

If you want to make the inputs of your resouce method manadatory then use the param type as @pathaparam, if the inputs of your resource method should be optional then keep that param type as @QueryParam param

i am giving one exapmle to undersand when do we use @Queryparam and @pathparam for example i am taking one resouce is carResource class if u want the inputs of ur resouce method is manadatory then use the param type as @pathaparam,if the inputs of ur resource method may be optional then keep that param type as @QueryParam param <pre><code> @Path("/car") class CarResource { @Get @produces("text/plain") @Path("/search/{carmodel}") public String getCarSearch(@PathParam("carmodel")String model,@QueryParam("carcolor")String color)  { //logic for getting cars based on carmodel and color ----- return cars } } </code></pre> for this resouce pass the request 

For this resouce pass the request

req uri ://address:2020/carWeb/car/search/swift?carcolor=red   if u give req like this the resouce will gives the based car model and color 

If you give req like this the resouce will gives the based car model and color

 req uri://address:2020/carWeb/car/search/swift if u give req like this the resoce method will display only swift model based car 

If you give req like this the resoce method will display only swift model based car

req://address:2020/carWeb/car/search?carcolor=red if u give like this we will get resourcenot found exception because in the car resouce class i delcared carmaodel as @pathPram that is u must and should give the carmodel as reQ uri othere wise it will not pass the req to resouce but if u don't pass the color also it will pass the req to resource y becoz the color is @quetyParam its is optioanl in req  i think this is help full for u 

If you give like this we will get ResourceNotFound exception because in the car resouce class I declared carmodel as @pathPram that is you must and should give the carmodel as reQ uri otherwise it will not pass the req to resouce but if you don't pass the color also it will pass the req to resource why because the color is @quetyParam it is optional in req.

i am giving one exapmle to undersand when do we use @Queryparam and @pathparam for example i am taking one resouce is carResource class if u want the inputs of ur resouce method is manadatory then use the param type as @pathaparam,if the inputs of ur resource method may be optional then keep that param type as @QueryParam param <pre><code> @Path("/car") class CarResource { @Get @produces("text/plain") @Path("/search/{carmodel}") public String getCarSearch(@PathParam("carmodel")String model,@QueryParam("carcolor")String color)  { //logic for getting cars based on carmodel and color ----- return cars } } </code></pre> for this resouce pass the request req uri ://address:2020/carWeb/car/search/swift?carcolor=red   if u give req like this the resouce will gives the based car model and color  req uri://address:2020/carWeb/car/search/swift if u give req like this the resoce method will display only swift model based car req://address:2020/carWeb/car/search?carcolor=red if u give like this we will get resourcenot found exception because in the car resouce class i delcared carmaodel as @pathPram that is u must and should give the carmodel as reQ uri othere wise it will not pass the req to resouce but if u don't pass the color also it will pass the req to resource y becoz the color is @quetyParam its is optioanl in req  i think this is help full for u 

I am giving one exapmle to undersand when do we use @Queryparam and @pathparam

For example I am taking one resouce is carResource class

If you want to make the inputs of your resouce method manadatory then use the param type as @pathaparam, if the inputs of your resource method should be optional then keep that param type as @QueryParam param

@Path("/car") class CarResource { @Get @produces("text/plain") @Path("/search/{carmodel}") public String getCarSearch(@PathParam("carmodel")String model,@QueryParam("carcolor")String color) { //logic for getting cars based on carmodel and color ----- return cars } } 

For this resouce pass the request

req uri ://address:2020/carWeb/car/search/swift?carcolor=red 

If you give req like this the resouce will gives the based car model and color

 req uri://address:2020/carWeb/car/search/swift 

If you give req like this the resoce method will display only swift model based car

req://address:2020/carWeb/car/search?carcolor=red 

If you give like this we will get ResourceNotFound exception because in the car resouce class I declared carmodel as @pathPram that is you must and should give the carmodel as reQ uri otherwise it will not pass the req to resouce but if you don't pass the color also it will pass the req to resource why because the color is @quetyParam it is optional in req.

Source Link
user4374220
user4374220

i am giving one exapmle to undersand when do we use @Queryparam and @pathparam for example i am taking one resouce is carResource class if u want the inputs of ur resouce method is manadatory then use the param type as @pathaparam,if the inputs of ur resource method may be optional then keep that param type as @QueryParam param <pre><code> @Path("/car") class CarResource { @Get @produces("text/plain") @Path("/search/{carmodel}") public String getCarSearch(@PathParam("carmodel")String model,@QueryParam("carcolor")String color) { //logic for getting cars based on carmodel and color ----- return cars } } </code></pre> for this resouce pass the request req uri ://address:2020/carWeb/car/search/swift?carcolor=red if u give req like this the resouce will gives the based car model and color req uri://address:2020/carWeb/car/search/swift if u give req like this the resoce method will display only swift model based car req://address:2020/carWeb/car/search?carcolor=red if u give like this we will get resourcenot found exception because in the car resouce class i delcared carmaodel as @pathPram that is u must and should give the carmodel as reQ uri othere wise it will not pass the req to resouce but if u don't pass the color also it will pass the req to resource y becoz the color is @quetyParam its is optioanl in req i think this is help full for u