gpt4 book ai didi

java - 有没有办法在 application.properties 文件中写入 JSON 数组列表(spring-boot)

转载 作者:行者123 更新时间:2023-12-01 18:05:50 25 4
gpt4 key购买 nike

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

下面是我的 json 对象。

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

如果我使用 jasypt 来编码密码,那么代码会是什么?

最佳答案

引用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;
}
}

关于java - 有没有办法在 application.properties 文件中写入 JSON 数组列表(spring-boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60561180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com