Skip to content

Instantly share code, notes, and snippets.

@fpopic
Last active August 2, 2026 06:29
Show Gist options
  • Select an option

  • Save fpopic/e6f021b42d21054199ad to your computer and use it in GitHub Desktop.

Select an option

Save fpopic/e6f021b42d21054199ad to your computer and use it in GitHub Desktop.
/*------------------------------------------------------------------------------------------------*/
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);
}
}
@fpopic

fpopic commented Jan 1, 2015

Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment