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

I am new with Backend, it's my first and I have to build REST API Service for my Android app (with mongodb as database), which will use it as REST Cient via Retrofit, I chose Spring cause it uses Java and it's familiar with retrofit syntax. I followed the sample https://github.com/cloudfoundry-samples/spring-music but I got lost as many others with MongoTemplate and MongoRepository
http://stackoverflow.com/questions/17008947/spring-data-mongotemplate-vs-mongorepository/

Can you tell me how to extend AlbumRepository with my custom methods from AlbumCustomRepository to make MyAlbumController working cause simple extend doesn't work ?

I didn't change anything from this spring-music sample, I thought JavaConfig is better than XML but much more confusing and if you follow different samples you got stuck like me :)

I pushed it to http://spring-music-underplain-picocurie.cfapps.io/

@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