我有一个简单的 Spring 应用程序。在那个应用程序中,我有 @Repository
,它应该提供对包含我的数据的 JSON
文件的访问。
@Repository("jsonStorageUserRepository")
public class JsonUserRepository implements UserRepository {
@Value("${users}")
private List<User> users;
@Override
public User getByCursor(Cursor cursor) {
return users.get(cursor.getId);
}
}
我的 users.json
文件(存储)在 properties
文件夹中 -
[
{
"name": "Bill",
"registered": true
},
{
"name": "John",
"registered": true
}
]
还有我的 DTO
类 -
public class User implements Serializable {
private boolean registered;
private String name;
public User(String name, boolean redistricted) {
this.name = name;
registered = redistricted;
}
// setters and getters were omitted
}
现在我有异常(exception):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonStorageUserRepository': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.springapp.mvc.repository.json.JsonUserRepository.users; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'users' in string value "${users}"
与这一行有关-
@Value("${users}")
private List<User> users;
是否可以从 JSON
连接转换后的实体列表?
最佳答案
读取 JSON 格式属性文件将不起作用。您需要编写自定义属性编辑器。看看那篇文章:
http://christopherhunt-software.blogspot.de/2010/04/spring-propertyeditor-for-json-map-and.html
关于java - 如何在 Spring 中从文件连接 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25586435/