I need to solve a learning task: read the contents of a file called users.json
and write it out to the console. Then, I need to use this data to add users to Tomcat
JsonConfig.java
@Configuration
@ComponentScan(basePackageClasses = {ru.vniiem.mcc.initstateservice.models.UserModel.class })
public class JsonConfig {
private static final Logger LOG = LogManager.getLogger(JsonConfig.class);
@Autowired
public UserModel[] usrModel;
@Bean
public void usersLog() {
LOG.info(usrModel);
}
}
UserModel.java
@Component
@PropertySource(value = "classpath:users.json", factory = JsonPropertySourceFactory.class)
@ConfigurationProperties
public class UserModel {
@Value("${login}")
private String login;
@Value("${password}")
private String password;
public UserModel() {
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JsonPropertySourceFactory.java
public class JsonPropertySourceFactory
implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(
String name, EncodedResource resource)
throws IOException {
Map readValue = new ObjectMapper()
.readValue(resource.getInputStream(), Map.class);
return new MapPropertySource("json-property", readValue);
}
}
users.json:
[
{
"login": "aaa",
"password": "aaa"
},
{
"login": "bb",
"password": "bbb"
}
]
When executing the code:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.LinkedHashMap<java.lang.Object,java.lang.Object>` from Array value (token `JsonToken.START_ARRAY`)
at [Source: (ByteArrayInputStream); line: 1, column: 1]
I think I need to fix the factory, but I don’t understand what I need to do, because PropertySource
accepts only Map
and `java.util.Properties.
Update 1
I changed method:
public class JsonPropertySourceFactory
implements PropertySourceFactory {
private static final Logger LOG = LogManager.getLogger(JsonPropertySourceFactory.class);
@Override
public PropertySource<?> createPropertySource(
String name, EncodedResource resource)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<Map<String, String>> usersList = mapper.readValue(resource.getInputStream(), new TypeReference<List<Map<String, String>>>() {});
Map<String, Object> usersMap = new HashMap<>();
for (int i = 0; i < usersList.size(); i++) {
Map<String, String> user = usersList.get(i);
usersMap.put("user" + (i + 1), user); // Ключ в формате "user1", "user2" и т.д.
}
System.out.println(usersMap);
return new MapPropertySource("json-property", usersMap);
}
}
On this stage I have a map that contains values, but I don’t know how I can manipulate with this. After my users.json is read, I need to work with this data and add it to Tomcat users for basic auth.
Olesky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4