SpringBoot:通过yml文件的ModelAttribute默认属性不起作用

时间:2018-07-14 09:23:32

标签: spring-boot properties yaml 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 Request的新代码

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
}

1 个答案:

答案 0 :(得分:0)

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) {}

}