I have example controller method
public void generateFile(@RequestBody final FileRequest request) {
...
}
Sometimes not all fields of this class FileRequest are filled, is there any way to set the default value when the value in the request is empty or null?
I mean something like @Default
In FileRequest class, setting field with a value. If field not filled, it will use default value in class. Use lombok, class is too simple. like as below:
// class User
import lombok.Data;
@Data
public class User {
private String name;
private String address="beijing";
private int age=10;
}
// in Class restConctroller
@RequestMapping(value = "/res1/data")
public Object postData(@RequestBody User user){
return user;
}
after post http://localhost:8080/res1/data with name='aaa', you will get result as
{
"name": "aaa",
"address": "beijing",
"age": 10
}
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