@RequestParam set default value
时间: 2023-10-30 08:05:32 浏览: 205
To set a default value for a request parameter in a Java Spring application, you can use the `@RequestParam` annotation along with the `defaultValue` attribute. Here's an example:
```java
@GetMapping("/example")
public String exampleMethod(@RequestParam(defaultValue = "default") String parameter) {
// Your code here
}
```
In this example, the `parameter` request parameter will have a default value of "default" if it is not provided in the request. You can replace "default" with any desired default value.
If the request contains the parameter, the provided value will be assigned to the `parameter` variable. Otherwise, it will use the default value.
Note that the `defaultValue` attribute only sets a default value when the parameter is not provided or is empty. If the parameter is provided with a non-empty value, that value will be used instead.
阅读全文