Retain Old configuration if new one is invalid
Retain Old configuration if new one is invalid
@Component
@ConfigurationProperties("person")
@RefreshScope
@Validated
public class PersonConfiguration
@NotBlank
public String name;
public String getName()
return name;
public void setName(String name)
this.name = name;
@RestController
class MessageRestController
@Autowired
private PersonConfiguration personConfig;
@RequestMapping("/message")
String getMessage()
return personConfig.getName();
Configuration in git:
person:
name: aaaa
I have a rest service and it has configuration which is read from git using spring cloud config.
In the configuration when name is not empty and when someone hits the /message
endpoint it's returning name correctly. If someone changes the name to an empty string and when /message
endpoint is called it's throwing a binding exception since name should not be blank.
/message
/message
If someone updates git config to an invalid value how can i hold off to the previous version of that config so that /message
endpoint would still be functional with previous valid config
/message
1 Answer
1
You could create validation logic within your PersonConfiguration.class in setter methods. For example, removing @Validated
and @NotBlank
you could just write:
@Validated
@NotBlank
@Component
@ConfigurationProperties("person")
@RefreshScope
public class PersonConfiguration
public String name;
public String getName()
return name;
public void setName(String name)
if (Strings.isBlank(name))
this.name = "default";
else
this.name = name;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not a current feature
– spencergibb
Sep 6 '18 at 0:42