Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send Boolean as pathvariable to controller

Is it possible and a good practice to send Boolean as pathvariable to controller in url? I am using spring 3.1 and been trying to send a boolean from Jsp to the controller as @Pathvariable("yesorNo") boolean yesOrNo. But keep getting error as the request is syntactically incorrect. Any insight?

like image 291
Java4life Avatar asked Aug 21 '12 21:08

Java4life


People also ask

How do you give a PathVariable URL?

The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.

Can request Param be Boolean?

The 'required' attribute of @RequestParamIt is Boolean type attribute whether the parameter is required. The default is true. If parameter is missing in the request then it will be returned status code 400. We can override this to false if the parameter is not present in the request.

What is the difference between RequestParam and PathVariable?

1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

Is PathVariable required by default?

7. @PathVariable variables are required by default. Below @GetMapping method handles multiple request paths, but the @PathVariable are required by default, which means if we access the request path /api2/page/ , Spring will return an HTTP 500 server error code and also error message like missing URI template variable.


1 Answers

Yes, you can, it would look like

@RequestMapping(value="value/{someVal}")
public void handleBooleanParameter(@PathVariable("someVal")boolean someVal){
   //do something
}

You would then access it with

http://<base url>/value/true

or

http://<base url>/value/false
like image 137
Chris Thompson Avatar answered Sep 21 '22 17:09

Chris Thompson