0

Age limit default value is not picked by ModelAttribute, However it was working fine with request parameter.

YML File

age:            
    default:
        limit:  60  

Below is old Code with Request Parameter Request

public ResponseEntity<Account> getPersonAccount(@RequestParam String name,@Min(value=0) @RequestParam(required = false, defaultValue = "${age.default.limit}") Integer limit
){
}

Below is new Code with 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
}
7
  • Have you look at this post? stackoverflow.com/questions/12296642/… Commented Jul 14, 2018 at 9:30
  • This post is different related to parameter only. I just want to initialize model attribute object's property from yml file. Commented Jul 14, 2018 at 10:18
  • You can't use yml properties anywhere and everywhere. "@Value" njects it into private variables via getters and setters. You can't assign defaultValue parameter in the "@RequestParam" just by putting the ${property} in there. Commented Jul 14, 2018 at 10:39
  • defaultValue = "${age.default.limit}" in request parameter was working fine from yml file. I only want to provide default value to new model object from yml file. I am looking solution for that only. getter/ setter are already there. Commented Jul 14, 2018 at 10:49
  • That's not how it works at all. For you to substitute a yml config item, you need '@Value'. You can't assign without using '@Value' and you can't use @Value in '@RequestParam' Commented Jul 14, 2018 at 12:56

1 Answer 1

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

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.