SpringBoot:ModelAttribute 默认 属性 通过 yml 文件不起作用
SpringBoot: ModelAttribute default property through yml file not working
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) {}
}
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) {}
}