Over a million developers have joined DZone.

Spring Data, MongoDB, and Spring Boot

You really can quickly develop a microservice to access MongoDB from Java 8 with little coding. Give it a try... easy like Sunday morning.

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

Image title

It’s so simple, I almost don’t believe it.  And, if this seems to hard, try using JHipster.

You really can quickly develop a microservice to access MongoDB from Java 8 with little coding.   Give it a try.  Java 8 + Spring Boot + Gradle + MongoDB + YAML.  Easy like Sunday morning.

Gradle

 compile(“org.springframework.boot:spring-boot-starter-data-mongodb”)

Application.yml

spring.profiles.default: local

Application-local.yml

spring.data.mongodb.database: my-db-engine
spring.data.mongodb.uri: mongodb://localhost:2701

Repository

import org.springframework.data.repository.CrudRepository;
import com.mysite.service.stuff.model.MongoModel;

public interface IMongoModelEnabledRepository extends CrudRepository<MongoModel, String>
{
    public MongoModel findByDomain(String domain);
}

Usage

@Autowired

IMongoModelEnabledRepository myrepo;

myrepo.findByDomain(domain);

Spring Boot Application

@PropertySource(“classpath:application.yml”)
@EnableAsync
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:
spring architecture ,spring data ,nosql ,mongodb ,gradle ,spring boot ,yaml

Published at DZone with permission of Tim Spann, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}