Spring Boot, MySQL and MongoDB

104 Views Asked by At

I have a working Spring Boot application with MySQL.

I decided to add Mongo data source to my application. I set up the configuration and everything. Then I noticed that some endpoints return the following error when I try to hit with a projection: ../some-endpoint?projection=projection_name. (If I use the same endpoint without the projection, it works.)

{
    "timestamp": "2023-09-28T17:59:42.525+00:00",
    "status": 500,
    "error": "Internal Server"
    "message": "Could not write JSON: Cannot get or create PersistentEntity for type com.sun.proxy.$Proxy280! PersistentEntities knows about 2 MappingContext instances and therefore cannot identify a single responsible one. Please configure the initialEntitySet through an entity scan using the base package in your configuration to pre initialize contexts.; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Cannot get or create PersistentEntity for type com.sun.proxy.$Proxy280! PersistentEntities knows about 2 MappingContext instances and therefore cannot identify a single responsible one. Please configure the initialEntitySet through an entity scan using the base package in your configuration to pre initialize contexts. (through reference chain: org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionResource[\"content\"]->com.sun.proxy.$Proxy275[\"interestedIndustry\"])",
    "path": "/api/session"
}

I don't get why.

The following is the configuration I set for this datasource:


import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Configuration
public class DatabaseConfiguration extends AbstractMongoClientConfiguration {

    @Value("${spring.data.mongodb.database}")
    private String database;
    @Value("${spring.data.mongodb.host}")
    private String host;
    @Value("${spring.data.mongodb.port}")
    private String port;
    @Value("${spring.data.mongodb.password}")
    private String password;
    @Value("${spring.data.mongodb.username}")
    private String username;

    private List<Converter<?, ?>> converters = new ArrayList<>();


    @Override
    public MongoClient mongoClient() {
        String connection = String.format("mongodb://%s:%s@%s:%s", username, password, host, port);
        return MongoClients.create(connection);
    }

    @Override
    protected boolean autoIndexCreation() {
        return true;
    }

    @Override
    public MongoCustomConversions customConversions() {
        //converters.add(new ZonedDateTimeReadConverter());
        //converters.add(new ZonedDateTimeWriteConverter());
        return new MongoCustomConversions(converters);
    }

    @Override
    protected String getDatabaseName() {
        return database;
    }

    @Override
    protected Collection<String> getMappingBasePackages() {
        return Stream.of("full.path.to.repositories").collect(Collectors.toList());
    }
}

Do you have any idea?

I tried to update my data source configuration class but without any success. Updates were about:

  • Setting @EnableMongoRepo... just after @SpringBootApplication
  • Updating my spring properties files
0

There are 0 best solutions below