Welcome back!  This tutorial will be a basic introduction to creating a Spring Boot application using IntelliJ IDEA.  No prior knowledge is expected, the main purpose of this post is to help anyone new to Spring get rolling quickly writing Spring applications with Spring Boot in IntelliJ.  For further reading, be sure to check out Spring’s Guide section for other tutorials on writing Spring Boot applications.

Update 2015-08-26: This tutorial has been updated with the latest version of IntelliJ 15 EAP at the time of writing and describes building an app with IntelliJ’s built in support for Spring Initializr.

Create your new project with IntelliJ

Open up IntelliJ and click on the Create New Project option.

Next, select Spring Initializr from the project type in the left panel, select your Project SDK and then click Next.  The Initializr Service URL should already be populated.

Next enter your Maven project properties including name, project type, packaging, Java version, group ID, artifact ID and version for your application.  Complete this step by entering a project description and root package.  Then click Next.

Next select your Spring Boot version and any Spring Framework dependency your project will require.  All of Spring’s projects are available from web, security, cloud, database and so on.  Click Next once you’ve selected all your dependencies.

The final step is entering your IntelliJ IDEA project settings.  Click Finish when complete.

At this point, IntelliJ may prompt you to add your pom.xml as a managed Maven project.  Click Add as Maven Project to dismiss this prompt and have your Maven dependencies automatically synced with your IntelliJ build path.  You’ll see that using Spring Boot, there’s many dependencies you don’t have to add yourself thanks to Spring Boot’s starter POMs.

Exploring the Project

Open up your project’s pom.xml and you’ll see what we just built.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.patrickgrimard</groupId>
    <artifactId>spring-boot-hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-hello-world</name>
    <description>Sample Spring Boot Project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

The generated application class

@SpringBootApplication
public class SpringBootHelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHelloWorldApplication.class, args);
    }
}

This class although very simple looking has a lot going on.  First, it’s been annotated with the @SpringBootApplication annotation. This declares the class as a @Configuration class for your application.  You can define additional @Bean definitions in it if you like.  It adds @ComponentScan to locate any other beans you define in your project.  Finally it adds @EnableAutoConfiguration in order to enable automatic configuration of your Spring application context by determining what you have available on your classpath.

Create a REST Controller

The next thing you may want to do at this point is add a controller to your application to handle servlet requests.

@RequestMapping("/api")
@RestController
public class WidgetController {

    @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    public Widget index() {
        return new Widget("green", 10, 7);
    }
}

The first annotation on this controller is @RequestMapping("/api") and it tells us that this controller will handle all requests beginning with /api in our application.  The second annotation @RestController tells Spring two things.  First this class is a controller bean that should be managed by Spring and will be picked up by component scanning.  Secondly, all methods inherently use @ResponseBody semantics which means the return value will become the body of our servlet response.

I’ve only defined a single method in this controller responding to the GET HTTP method, and it produces JSON output.  My method merely returns a new instance of the Widget class.  Your controller will return something else that’s related to your business case.

Running your Application

At this point, you can run your application simply by right clicking inside your application class and selecting Run SpringBootHelloWorldApplication.main().  Then browse to http://localhost:8080/api and you’ll see the response from the controller and request mapping we just created.

Wrapping Up!

By now, you should know how to create a new Spring Boot application from scratch using IntelliJ, it’s really quite simple with the introduction of Spring Initializr built into the IDE.  I’ve also provided a very brief introduction to writing a Spring Boot app to get you started.

If you’d like to view the source code of this application, it’s available on Github https://github.com/pgrimard/spring-boot-hello-world.

Posted by Patrick Grimard

Leave a Reply