spring-boot - SpringBoot:通过yml文件的ModelAttribute默认属性不起作用
问题描述
年龄限制默认值不是由 选择的ModelAttribute
,但是它与请求参数一起工作正常。
YML 文件
age:
default:
limit: 60
下面是带有请求参数请求的旧代码
public ResponseEntity<Account> getPersonAccount(@RequestParam String name,@Min(value=0) @RequestParam(required = false, defaultValue = "${age.default.limit}") Integer limit
){
}
下面是带有 ModelAttribute 请求的新代码
public ResponseEntity<Account> getPersonAccount(@ModelAttribute("person" ) Person person) {}
@Configuration
public class Person implements Serializable {
private String name;
@Value("${age.default.limit}" )
private Integer limit;
getter/setter
}
解决方案
public class PersonController {
@Value("${age.default.limit}" )
private Integer limit;
@ModelAttribute("person")
public Person populatePerson() {
Person person = new Person();
person.setLimit(limit);
return user;
}
public ResponseEntity<Account> getPersonAccount(@ModelAttribute("person" ) Person person) {}
}
推荐阅读
- python - 蟒蛇 | MYSQL - 错误 1064:您的 SQL 语法有错误
- vue.js - Vue 在输入文本上,更改两个输入字段的背景颜色
- networking - 访问 Kubernetes 集群中的特定 Pod
- javascript - 如何动画菜单背景更改图像?
- php - 如何从 Laravel 将大文件上传到 Amazon S3?
- apache-kafka - kafka broker长时间incative和几天后启动有什么影响?
- outlook - 共享 Outlook 日历而不显示详细信息?
- mysql - 突然,我的实例停止响应 mysql 连接的传入连接
- sql - 如何在 SQLite 中显示我的表/关系的属性?
- c++ - 使用派生类的静态成员而不是基类