So, I am building out architecture for user functionality in a new app, written in Java 17 and Spring Boot 3.2.5. I’m consistently getting error logs containing the message above.
Here is the domain object I’m using, less getters and setters:
public class User extends RepresentationModel<User> {
private final String email;
private final String password;
private final String firstName;
private final String lastName;
@JsonCreator
public User(@JsonProperty("email") String email, @JsonProperty("password") String password, @JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
}
}
I’m using Mongo for my persistence layer, and using a bean to assemble a MongoTemplate from the required, overridden methods in the extended, abstract Mongo configuration interface:
@Configuration
public class MongoGeneral extends AbstractMongoClientConfiguration {
@Value("${spring.data.mongodb.username}")
private String username;
@Value("${spring.data.mongodb.password}")
private String password;
@Override
@NonNull
protected String getDatabaseName() {
return "flawless-beans";
}
@Override
@NonNull
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString("mongodb+srv://" + username + ":" + password + "@cluster0.2ahn6zq.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0");
System.out.println(username+", "+password);
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
@Bean(name = "mongoTemplate")
MongoTemplate mongoTemplate() {
return new MongoTemplate(new SimpleMongoClientDatabaseFactory(mongoClient(), getDatabaseName()));
}
}
Just to do a conceptual task, I’m using the main class to do a pull for all the objects; I’ll remove it when I’m ready to abstract things out:
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MongoGeneral.class);
MongoTemplate mongoTemplate = context.getBean("mongoTemplate", MongoTemplate.class);
mongoTemplate.findAll(User.class);
SpringApplication.run(FlawlessBeansJavaSpringApiApplication.class, args);
}
Problem is, I keep getting these errors, and neither Google nor experimentation have helped:
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make protected java.nio.charset.Charset(java.lang.String,java.lang.String[]) accessible: module java.base does not "opens java.nio.charset" to unnamed module @55141def
at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:391)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:367)
There are many more lines after that, but none under new headings; this seems to be an error with many causes, so research hasn’t born fruit. However, these are the last three lines:
at org.springframework.data.mongodb.core.MongoTemplate.getCollectionName(MongoTemplate.java:505)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1852)
at com.mistermorse.flawlessbeansjavaspringapi.FlawlessBeansJavaSpringApiApplication.main(FlawlessBeansJavaSpringApiApplication.java:17)
This last log is the line executing mongoTemplate.findAll(User.class)
; I’m not surprisedby this, because the app starts fine when that line is commented out, and only breaks on re-addition.
I can start the app without console errors even when I purposely break the config password, so I expect connection errors, but this seems local (and these errors, if Google results are to be considered, occur in lots of places).