【问题标题】:Is there any way to write JSON array list in application.properties file (spring-boot)有什么方法可以在 application.properties 文件中写入 JSON 数组列表(spring-boot)
【发布时间】:2020-03-06 09:33:15
【问题描述】:

有没有办法在application.properties文件(spring-boot)中写入Json数组列表,这样我就可以用最少的代码读取整个Json?

下面是我的 json 对象。

{ "userdetail": [
    {
      "user": "user1",
      "password": "password2",
      "email": "email1"
    },
    {
      "user": "user2",
      "password": "password2",
      "email": "email2"
    }]}

如果我使用 jasypt 对密码进行编码,那么密码是什么?

【问题讨论】:

    标签: java json spring spring-boot application.properties


    【解决方案1】:

    参考Spring Boot Docs here

    你可以使用 YAML

    my:
       servers:
           - dev.example.com
           - another.example.com
    

    或普通的配置属性数组:

    my.servers[0]=dev.example.com
    my.servers[1]=another.example.com
    

    您可以通过这种方式将这些属性加载到应用程序中:

    @ConfigurationProperties(prefix="my")
    public class Config {
    
        private List<String> servers = new ArrayList<String>();
    
        public List<String> getServers() {
            return this.servers;
        }
    }
    

    对于您的情况,我会尝试类似:

    users.userdetail[0].user=..
    users.userdetail[0].password=..
    ...
    

    并通过阅读

    @ConfigurationProperties(prefix="users")
    public class Userdetail {
    
        private class User {
             public String user;
             public String password;
             public String email;
        }  
    
        private List<User> userdetails = new ArrayList<User>();
    
        public List<User> getUserdetails() {
            return this.userdetails;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2017-10-04
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多