Skip to content

Instantly share code, notes, and snippets.

@arthurportas
Last active November 17, 2021 11:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arthurportas/bd3b2a84d109be6737df to your computer and use it in GitHub Desktop.
Save arthurportas/bd3b2a84d109be6737df to your computer and use it in GitHub Desktop.
Sample spring mongodb project setup with mongodb custom converters
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableMongoRepositories(basePackages = "com.arthurportas.repo.mongo")
@ComponentScan(basePackages = "com.arthurportas")
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Value("${mongo.host}")//read from config
private String host;
@Value("${mongo.port}")//read from config
private int port;
@Value("${mongo.db}")//read from config
private String databaseName;
@Value("${mongo.username}")//read from config
private String userName;
@Value("${mongo.password}")//read from config
private String password;
private final List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
@Override
public String getDatabaseName() {
return databaseName;
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new MongoClient(serverAddress(), Arrays.asList(mongoCredential()));
}
@Override
protected String getMappingBasePackage() {
return "com.arthurportas.domain.mongo.entities";
}
@Bean
public ServerAddress serverAddress() throws Exception {
return new ServerAddress();//TODO: read host+port from config
}
@Bean
public MongoCredential mongoCredential() {
return MongoCredential.createCredential(
this.userName,
getDatabaseName(),
this.password.toCharArray());
}
@Override
public CustomConversions customConversions() {
converters.add(new BigDecimalToDoubleConverter());
converters.add(new DoubleToBigDecimalConverterp());
return new CustomConversions(converters);
}
}
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
public class BigDecimalToDoubleConverter implements Converter<BigDecimal, Double> {
@Override
public Double convert(BigDecimal source) {
return source.doubleValue();
}
}
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
@Component
public class DoubleToBigDecimalConverter implements Converter<Double, BigDecimal> {
@Override
public BigDecimal convert(Double source) {
return new BigDecimal(source).setScale(2, RoundingMode.HALF_UP);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment