DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot Mongo DB Example Application [Video]

Spring Boot Mongo DB Example Application [Video]

Creating example applications can be helpful when you want to be sure that your app will make proper use of Spring Boot when integrated with Mongo DB.

John Thompson user avatar by
John Thompson
·
Feb. 17, 17 · Presentation
Like (3)
Save
Tweet
Share
7.22K Views

Join the DZone community and get the full member experience.

Join For Free

Transcript

I’d like do a quick code review of my Spring Boot Mongo DB example application. This is up on GitHub and you can find it under my repository Spring framework Guru/spring — boot — Mongo DB. (Pretty creative name there.)

This is an example Spring Boot Application connect to Mongo DB, not necessarily running in Docker. I connect can connect to any Mongo DB database.

So, let’s take a quick look at the code in it and do a quick overview. I already have it loaded up in IntelliJ.

So, let's start up from the domain up. What I have is a product, and this is a standard mapping class for Spring, mapping out to a Mongo document. So, no big mystery there. We just have a product class with an ID, description, price, and image URL. So, nothing terribly exciting there.

Now I do have a couple converters, so I am using a form, a command form; some people call it command object to back it. So, this converts it back and forth. So, this my product form. It's a command. So, really the biggest difference there is that we are treating the ID as a string because Mongo database type does not really transfer over to the web tier very well. So, we do need to convert that back-and-forth.

So, next thing to look at our standard Spring MVC controller. This is the controller going to facing out to the web and handling web requests for us.

I did jump up a little bit, and if you’re following along, this pattern is gonna look very very familiar. So, I have a service layer that interacts with my controller layer. So this is my interface for the service. Then there’s my implementation of it. And I am using spring data repositories for this and almost all the wiring in my converter saw a wire in the two dependencies one is the product repository which is provided by spring data and this is the Mongo implementation that I am using and everything is wired up and then finally we have a couple time leaf templates here that we used to show the data so Megan I get into all the all the details here but you can see what’s going on here so nothing

I am using Spring Data Repositories for this and also wiring in my converter. I wire in the two dependencies. One is the product repository, which is provided by Spring Data, and this is the Mongo DB implementation that I am using. Everything is wired up.

And then finally, we have a couple Thymeleaf templates here that we used to show the data from Mongo. I’m not going to get into all the all the details here — but you can see what’s going on here. 

So, nothing too terribly creative here. It's just a quick and dirty to get this working.  It is not production grade by any means.

And then, let’s take a quick look at the dependencies. Up, you can see there on line 17, I am fact running Spring Boot 1.5.1, and that is the most current release of Spring Boot at the time of recording.

I’m bringing in a couple other dependencies, and these are important. So bring in the Spring Boot Starter Data for Mongo DB. As well as Thymeleaf, web, and of course the test.

And this POM is fairly untouched since I pulled it off of Josh Long’s favorite web site spring.start.io. I’m sorry start.spring.io. And that’s the URL so you can grab this at any time.

So, let's go ahead and take this for a spin. What I’m going to do, I have command line ready over here.

And I have Docker there so that’s a standard run command for Docker. Docker run map out the ports for the latest image of Mongo. And the minus D parameter tells it to run it in the background.

So, that is now running Docker. Use the command Docker PS see that it is running.

And let’s do Docker logs minus F with the image name here. So now you can see that it is running. Let’s bounce over to InteliJ and I am going to start up my Spring Boot application. We can see Spring Boot is initializing. Pretty light project, so it's going to come up pretty quick and it's running on Tomcat now. Look at the other window you see that I have a new connection established to Docker (to Mongo). I don’t have a log level turned up on this so we won’t see any database activity.

Let’s come over here and to localhost 8080 and that’s going to do a redirect to a product list. I did not initialize any data in the database, but I can come and create a new product. Product $22 and URL in URL. It’s not doing any data type checking there. It will show me that this is created. This is the Mongo ID that was created. So, now, I come back over here I see that it is listed. And should I want to edit it, and say do new product 2222. I am going to submit it, and we see that that is been updated. If I come back to the index again it redirects to product slash list. And I don’t have too much interesting here, but it shows that at that update has been persisted and is getting pulled out of the Mongo database.

Spring Framework Spring Boot application

Published at DZone with permission of John Thompson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Web Application Architecture: The Latest Guide
  • A Complete Guide to AngularJS Testing
  • Why Every Fintech Company Needs DevOps
  • Kotlin Is More Fun Than Java And This Is a Big Deal

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: