【问题标题】:Retain Old configuration if new one is invalid如果新配置无效,则保留旧配置
【发布时间】:2019-02-11 04:15:56
【问题描述】:
@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();
    }
}

git 中的配置:

person:
    name: aaaa

我有一个休息服务,它有使用 spring cloud config 从 git 读取的配置。 在配置中,当名称不为空并且有人点击/message 端点时,它会正确返回名称。如果有人将名称更改为空字符串,并且在调用 /message 端点时,它会引发绑定异常,因为名称不应为空。

如果有人将 git 配置更新为无效值,我如何才能推迟到该配置的先前版本,以便 /message 端点仍然可以使用先前的有效配置

【问题讨论】:

  • 不是当前功能

标签: spring spring-boot spring-cloud spring-cloud-config


【解决方案1】:

您可以在您的 PersonConfiguration.class 中的 setter 方法中创建验证逻辑。例如,删除 @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;
        }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多