Last active
August 2, 2026 06:29
-
-
Save fpopic/e6f021b42d21054199ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*------------------------------------------------------------------------------------------------*/ | |
| public interface AlbumRepository extends CrudRepository<Album, String>, AlbumRepositoryCustom { | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Repository | |
| public interface MongoAlbumRepository extends MongoRepository<Album, String>, AlbumRepository { | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Configuration | |
| @Profile("mongodb") | |
| @EnableMongoRepositories(basePackageClasses = {MongoAlbumRepository.class}) | |
| public class MongoConfig { | |
| @Bean | |
| public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) { | |
| return new MongoTemplate(mongoDbFactory); | |
| } | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Configuration | |
| @Profile("mongodb-cloud") | |
| public class MongoCloudConfig extends AbstractCloudConfig { | |
| @Bean | |
| public MongoDbFactory mongoDbFactory() { | |
| return connectionFactory().mongoDbFactory(); | |
| } | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Configuration | |
| @Profile("mongodb-local") | |
| public class MongoLocalConfig { | |
| @Bean | |
| public MongoDbFactory mongoDbFactory() { | |
| try { | |
| return new SimpleMongoDbFactory(new MongoClient(), "music"); | |
| } catch (UnknownHostException e) { | |
| throw new RuntimeException("Error creating MongoDbFactory: " + e); | |
| } | |
| } | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| public interface AlbumRepositoryCustom { | |
| List<Album> findByReleaseYearGreaterThan(String releaseYear); | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Component | |
| public class AlbumRepositoryImpl implements AlbumRepositoryCustom { | |
| private MongoOperations ops; | |
| @Autowired | |
| public AlbumRepositoryImpl(MongoOperations ops) { | |
| MongoTemplate mongoTemplate = null; | |
| Assert.notNull(ops, "MongoOperations must not be null!"); | |
| this.ops = ops; | |
| } | |
| @Override | |
| public List<Album> findByReleaseYearGreaterThan(String releaseYear) { | |
| List<Album> l = new ArrayList<>(); | |
| for (Album album : ops.findAll(Album.class)) { | |
| if (Integer.parseInt(album.getReleaseYear()) > Integer.parseInt(releaseYear)) { | |
| l.add(album); | |
| } | |
| } | |
| return l; | |
| } | |
| } | |
| /*------------------------------------------------------------------------------------------------*/ | |
| @Controller | |
| @RequestMapping(value = "/albums") | |
| public class MyAlbumController { | |
| private static final Logger logger = LoggerFactory.getLogger(MyAlbumController.class); | |
| private CustomMongoAlbumRepositoryImpl repository; | |
| @Autowired | |
| public AlbumController(AlbumRepository repository) { | |
| this.repository = repository; | |
| } | |
| @ResponseBody | |
| @RequestMapping(method = RequestMethod.GET, value = "/{releaseYear}", produces = MediaType.APPLICATION_JSON_VALUE) | |
| public List<Album> getById(@PathVariable String releaseYear) { | |
| logger.info("Getting album " + releaseYear); | |
| return repository.findByReleaseYearGreaterThan(releaseYear); | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry man, it's working my Intellij/Tomcat was wrong configured.
Thanks for your help on stackoverflow answers, now I Will read the whole reference for queries to imporve it.
http://spring-music-underplain-picocurie.cfapps.io/albums/q?releaseYear=1990