REST (JAX-RS) Spring Boot Base Project, Setup through IntelliJ IDEA

How-to: How to setup a REST (JAX-RS: Jersey) Spring Boot base project with IntelliJ IDEA

I spent the weekend reminding myself how to setup a Spring Boot project for a REST service that I will be creating for a side project at home. Every time you expect something to work right out of the box, there is always some nuance that sneaks up on you. I suppose this is a side effect of technology changing so often. I thought I would keep track of each step so it might be helpful for someone else.

Step 1- Create your base project

You can go directly to Spring Initializr to generate a new Spring Boot maven project with your desired dependencies. Click “switch to full version” to see all of the options.

If you use IntelliJ, you can also use the wizard plugin for Spring Initializr. This article will cover this approach.

From within IntelliJ, select FILE | NEW | PROJECT to open the New Project Wizard. On the left side, you should see an entry “Spring Initializr”. Choose this to see the following screen.

POM Artifact screen

Click next to see the following screen. On this screen, you will be filling out basic POM artifact information for your project.

Fill in the group name. I used “com.familyassist” as the service I am building is for this namespace of a side product I am working on.

The artifact this project will create for me is a messaging service, so I gave the artifact name as “messaging-service”.

Click Next when you are done.

Spring Initializr screen

Now here is the main screen that lets you choose what dependencies you will need in your Spring Boot project. I am checking the following as I know I will need these dependencies later.

If you have never used Lombok, you are missing out. Do yourself a favor and check it out.

  • Core: Security, Lombok
  • Web: Web, Jersey (JAX-RS)
  • NoSQL: Embedded MongoDB
  • Ops: Actuator, Actuator Docs

I would recommend for now while trying out this How-To, that you only add the items I added. Every new item you add could cause just one more setup issue that needs to be resolved. Try this How-to out without choosing extra and once you have it down then you can add to your POM later or create a new project with everything you need.

Click Next to move to the next screen.

Project Name screen

Now you can specify the Project name and where you would like to save the project. I named the project “messaging-service”. Click Finish to create your project.

Your newly created project workspace

You should now have a newly created project open. You should see an application class under the package you specified in your POM screen. I have one called MessagingServiceApplication. It is the Spring Boot main class that contains the SpringBootApplication annotation. This annotation drives many other features supplied by other annotations all-in-one. You can read more at Spring Boot Guide

Run your application

Try running your application by pressing the Play icon at the top of your IDE. Is should have automatically created a Run-Configuration for the newly created project and selected it automatically at the top when you chose Finish earlier. See the “play” triangle next to “MessagingServiceApplication” at the top middle of the IDE screen.

I was expecting Tomcat to run my site with the Actuator framework working right out of the box. Well, it did not quite happen that way. See in the screenshot below how I have a “Process finished with exit code 0” at the bottom of the Run console.

Note: This problem occurs on Spring Boot 1.5.2 which happened to be the version used for this tutorial. I believe it was fixed in Spring Boot 1.5.3. If this does not happen as you walk through this tutorial, just skip ahead to “Stop your application and let’s change one more …”.

This is one of those nuances I was referring to earlier. I was expecting it to pull what it needed to run Tomcat embedded automatically. The default for the “spring-boot-start-tomcat” dependency in the POM file has a scope of “provided”. Remove this line and try to run it again.

Now, you should see that it started your application in so many seconds.

Stop your application and let’s change one more setting before testing out the Actuator URLs. Go to your application.properties and add these lines. This will make all of the Actuator URLs available on port 9001 and your REST URLS will be available on port 8080.

Application.properties – Lines to change the management port:

server.port=8080
management.port=9001
management.address=127.0.0.1

Start your application again and try opening your website at http://localhost:9001/health. This is the Spring Actuator health check URL. You should see the following JSON. If so, you are up and running!

{
  status: "UP"
}

If you try to go the Actuator’s metrics URL, it may ask for your login information. http://localhost:9001/metrics

If so, then add the following lines to your application.properties to set a login and password for your entire site or to disable the Spring Security for now until you are ready to set it up for your services.

Application.properties to setup password:

security.user.name=admin
security.user.password=password

Application.properties to disable security:

management.security.enabled=false
security.basic.enabled=false

The metrics screen should look like the following. Once you change your application.properties. You will have to restart the service for it to take effect.

http://localhost:9001/health

Setup your Jersey configuration and your first REST endpoint next. Instructions are at the following link.

Next : Setup your Jersey Resource Config and Endpoint/Resource
Next : Setup Swagger to document your endpoints

Leave a Reply