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?
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.
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.
1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With