Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give default value as integer in @RequestParam()

I'm new to spring boot and learning @RequestParam()

I know that we can give defaultValue in String but when I am trying to give default value as Integer it's showing me an error.

@RequestMapping("/returnVeriable")
public int getVeriable(@RequestParam(required=true,defaultValue=1/*error here*/) int veri){
    return veri;
}

any help would be appreciated.

like image 655
Siddhesh Avatar asked Dec 14 '17 12:12

Siddhesh


People also ask

How do I set default value in RequestParam?

By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).

Is RequestParam required by default?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

What is the default value of a integer data member?

In the . NET Framework, types have a concept of default values. For example, for any reference type the default value is null , and for an integer type it is zero.

What is the difference between PathVariable and RequestParam?

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

What is the default value of @requestparam?

The ‘defaultValue’ attribute of @RequestParam It is String type attribute and the default value to use as a fallback when the request parameter is not provided or has an empty value. Providing a default value implicitly sets ‘required’ to false. @RequestParam (defaultValue="www.dineshonjava.com") String siteName

How to use @requestparam annotation in spring?

In spring the @RequestParam annotation is used to bind request parameter values to the handler method arguments in controller. Let’s see use of it with example in this article. defaultValue– It is String type attribute and the default value to use as a fallback when the request parameter is not provided or has an empty value.

What is a request parameter?

Request parameters are used to send additional information to the server. A URL contains these parameters. There are two types of parameters: Query Parameter: These are appended to the end of the request URL, Query parameters are appended to the end of the request URL, following '?' and listed in key-value pairs, separated by '&' Syntax:

How do I bind a request parameter to a method variable?

public String processForm (@RequestParam (defaultValue="Guest") String name, @RequestParam (required = false) String adult) { With the @RequestParam annotation, we bind the request parameter to the method variable. The defaultValue option gives a default value if the parameter is not available (the text input was left empty).


3 Answers

Try with "" around integer to make it string as the defaultValue is implemented as String.

@RequestMapping("/returnVeriable") public int getVeriable(@RequestParam(required=true,defaultValue="1") Integer veri){     return veri; } 

refer : https://jira.spring.io/browse/SPR-5915

like image 140
Ajay Dsouza Avatar answered Sep 19 '22 12:09

Ajay Dsouza


When a value traffic by HTTP protocol, it has no type. It's a String. And so it is at this parameter type at the annotation. Then, you do something like this:

@RequestParam(required = true, defaultValue = "1") Integer veri

And it will work fine.

like image 29
Fabiano Avatar answered Sep 20 '22 12:09

Fabiano


This should work

@RequestMapping("/returnVariable")
public int getVariable(@RequestParam(required=true,defaultValue="1") int var) {
    return var;
}

By default what ever you pass to the controller is treated as String and converted to respective types. So even default values need to be set as String

like image 29
pvpkiran Avatar answered Sep 20 '22 12:09

pvpkiran