Okay, 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 Bood 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, lets start up from the domain up. And 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. Its 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 Mongol 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

And 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

So nothing too terribly creative here. Its 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 I’m sorry And that’s the URL so you can grab this at anytime.

So lets go ahead and take this for 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 its going to come up pretty quick. and is 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 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.


Out of the box, Spring Boot is very easy to use with the H2 Database. Spring programmers typically prefer writing code against such lightweight in-memory database, rather than on an enterprise database server such as Microsoft SQL Server or Oracle.

In-memory databases come with several restrictions making them useful only in the development stages in local environments. While in-memory databases are great to develop against, data is not persisted to disk, thus is lost when the database is shut down.

As the development progresses, you would most probably require an RDBMS to develop and test your application before deploying it to use a production database server. I have written a series of posts on integrating Spring Boot for Oracle, MySQL, MariaDB, and PostgreSQL.

Spring makes switching between RDBM’s simple. When you’re using Spring Data JPA with an ORM technology such as Hibernate, the persistence layer is nicely well decoupled. Which allows you to easily run your code against multiple databases. The level of decoupling even allows you to easily switch between an RDBMS and a NoSQL database, such as MongoDB. One of my previous post on Integrating Spring Boot for MongoDB covers that.

In this post, I will discuss Spring Boot configuration for Microsoft SQL Server.

SQL Server Configuration

For this post, I’m using SQL Server 2014 Express installed locally on my laptop. I used SQL Server 2014 Management Studio to connect to the database server using SQL Server Authentication.
Connect To SQL Server
Once you are logged in, create a springbootdb database from the Object Explorer window.
Configure SQL Server database for use with Spring Boot

A common problem that trips up many Java developers trying to connect to SQL Server is this error: The TCP/IP connection to the host localhost, port 1433 has failed. Error: “Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.”.

I too learned the hard way to resolve it with these steps.

    1. From the Start menu, open SQL Server 2014 Configuration Manager.
    2. Click Protocol for SQLEXPRESS under SQL Server Network Configuration on the left pane. On the right pane, right- click TCP/IP, and select Properties.
    3. On the TCP/IP Properties dialog box that appears, click the IP Addresses tab.
    4. Scroll down to locate the IPALL node. Remove any value, if present for TCP Dynamic Ports and specify 1433 for TCP Port.

TCP/IP Properties for SQL Server

  1. Click OK.
  2. Again right-click TCP/IP on the right pane, and select Enable.
  3. On the SQL Server Services node, right-click SQL Server (SQLEXPRESS), and select Restart.

This sets up SQL Server to be reached from JDBC code.

SQL Server Dependencies

To connect with SQL Server from Java applications, Microsoft provides a Microsoft JDBC Driver for SQL Server. However, till November 2016, Maven did not directly support the driver as it was not open sourced. By making it open source, Microsoft finally made the driver available on the Maven Central Repository. More information can be found here.

The Maven POM file of my Spring Boot application that brings in the database driver is this.


Spring Boot Properties

We need to override the H2 database properties being set by default in Spring Boot. The nice part is, Spring Boot sets default database properties only when you don’t. So, when we configure SQL Server for use, Spring Boot won’t setup the H2 database anymore.

The following data source configurations are required to configure SQL Server with Spring Boot.

As we are using JPA, we need to configure Hibernate for SQL Server too. Line 7 tells Hibernate to recreate the database on startup. This is definitely not the behavior we want if this was actually a production database You can set this property to the following values: none, validate, update, create-drop.

For a production database, you probably want to use validate.

Spring Framework 5
Become a Spring Framework 5 Guru!

JPA Entity

In our example application, we will perform CRUD operations on a user. For that, we will write a simple JPA entity, User for our application. I have written a post to use Spring Data JPA in a Spring Boot Web application, and so won’t go into JPA here.

JPA Repository

Spring Data JPA CRUD Repository is a feature of Spring Data JPA that I extensively use. Using it, you can just define an interface that extends CrudRepository to manage entities for most common operations, such as saving an entity, updating it, deleting it, or finding it by id. Spring Data JPA uses generics and reflection to generate the concrete implementation of the interface we define.

For our User domain class we can define a Spring Data JPA repository as follows.

That’s all we need to setup in Spring Boot to use SQL Server.

Let’s write some test code for this setup.

For the test, I have used JUnit. To know more about JUnit, you can refer my series on JUnit Testing.

The result of the JUnit test is this.

JUnit Test Result for SQL Server


As you can see, it is very easy to configure Spring Boot for SQL Server. As usual, Spring Boot will auto configure sensible defaults for you. And as needed, you can override the default Spring Boot properties for your specific application.