Spring Boot – Simple Maven Application

You know the basic of a programmers life. A lot of configuration stuff, which doesn´t want to work as you want and as it should. A lot of configuration makes it hard to focus on the business logic (Aspect oriented programming) and in a project it is very time intensive.

For example, think about implementing a REST-Service with JAX-RS. You need to know a lot of annotations and so one. Further more you don´t want to handle your object life-cycles by your self (or in a “complicated” way as it is with EJBs and/or in CDI).

That´s the reason why you should have a look at the Spring Boot Framework. Let´s get it on.

Setting up the Spring Boot Dependencies

First just create a simple Maven Project. Have you done this, open the pom and add the following dependencies,  plugins and the parent-directory:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
</parent>

<properties>
        <java.version>1.8</java.version>
</properties>

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

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

Maybe it is necessary to update your maven repository for using this dependencies. You can just do it by executing a clean install in your project directory

Implement our POJO Entity

There´s nothing special about this. You can use what ever you want as Entity, I´m choosing my Hero and create that entity in the entity package.

package com.entity;
/**
 *
 * @author javadevcorner.com
 */
public class Hero {

    private int id; 
    private String heroName;

    //default constructor, setter and getter
    
}

Create the Hero Boundary

Now we need our REST-Boundary. For this, we create a package called boundary and create the following class

package com.boundary;

import com.entity.Hero;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author javadevcorner.com
 */
@RestController
public class HeroController {

    @RequestMapping("/myHero")
    public Hero myHero() {
        Hero hero = new Hero();
        hero.setId(1);
        hero.setHeroName("Son Goku");

        return hero;
    }
}

That´s the point where we come in contact with the need Spring Boot Annotations for the first time. Let me explain them to you:

  • @RestController
    • We just need this annotation to tell the Spring Framework that the class HeroController is ready to handle Web-Requests and Responses.
    • Also combines the two annotations @Controller and @ResponseBody
  • @RequestMapping
    • This Annotation is our “equivalent” to @Path from JAX-RS calls. It just describes the URL to our Method.
      You can omit the (“/myHero”) than you have to omit it in the URL either

Implementing our Spring Application

Now we create a class called Application. Just put it in the root directory of your project. just right under src/main/java. No package as boundary, control, entity or something like that.

package com;

import java.util.Arrays;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("Spring Boot show me all your provided Beans:");
            List beanNames = Arrays.asList(ctx.getBeanDefinitionNames());
            beanNames.stream().sorted().forEach(bean -> System.out.println(bean));
        };
    }
}

That´s it. That´s the Spring Application. Let´s get it clear what is happening.

  • @SpringBootApplication is a convenient annotation which defines a few things for us.
    For example, it automatically discovers all the beans in the application or scans our classpath
  • @Bean is the method that will be called at startup of the application. The implementation of this method just prints out all beans that are discovered by our application.
    For this I am using the Java 8 Stream API.
Final project structure

Start and watch our Application

Open a Command Line Interface (for me the Git Bash) in the top-level directory of your project.

And just execute the following line

mvn package && java -jar target/firstSpringApplication.jar

If you named your Application (or the <finalName> attribute in your pom.xml) different, of course you have to change the artifact name after target/.

If you get an exception cause the default Port 8080 is already in use, please change the default Port.

Now there will be a lot of output on your console. The Beans what are discovered by our application. Just as we implemented.
In the same step, a local Tomcat Server will be started on default port 8080 so we can watch our Rest Service:

 

So thanks for reading and have fun starting with Spring Boot.

Feel free to share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.