Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.

Spring Boot

How to write a simple Spring Boot appllication

Spring Boot

How to write a simple Spring Boot application

“Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring. Spring Boot takes an opinionated view of building production ready applications.” – Pivotal.

Over the years Spring Boot has become an alternative and competitor to Java EE.
In this tutorial you will deploy the easiest Spring Boot application possible.

At the end of this tutorial you will have developed and deployed a Spring Boot “Hello World” example.

Creating the Project

We will use Maven for this tutorial so create a simple Maven Project

Now add the following dependencies and plugins to your pom.xml

xmlns="" xmlns:xsi=""




First lets create a simple POJO we want our REST-Service to return

public class Blog {
  private String name;
  private String url;
  public String getName() {
    return name;
  public void setName(String name) { = name;
  public String getUrl() {
    return url;
  public void setUrl(String url) {
    this.url = url;

Add a new class to your project and call it and implement the following code

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

class BlogResource{
 public Blog getBestBlog() {
 Blog bestJavaRelatedBlog = new Blog();
 return bestJavaRelatedBlog;


The @RestController annotation combines two combines @Controller and @Responsebody and will return data from the blog object in json.

Last but not least we need to implement the “main” class.  It is used by Spring to collect certain data that it needs to wire up your application

import java.util.Arrays;
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;

public class Application {

    public static void main(String[] args) {, args);

    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            for (String beanName : beanNames) {




Running the application

Go to your project destination folder and open up your favourite command line and type in

mvn spring-boot:run

Open up your browser and go to http://localhost:8080/bestblog

make sure to use the port 8080. I used 8081 cause 8080 was already in use.

Spread the love

Leave a Reply

Theme by Anders Norén