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="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">
4.0.0
com.javacodingnerd.tut
springboot-simple
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
2.0.0.RELEASE
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
2.0.0.RELEASE

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) {
this.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 BlogResource.java and implement the following code

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class BlogResource{
@RequestMapping("/bestblog")
public Blog getBestBlog() {
Blog bestJavaRelatedBlog = new Blog();
bestJavaRelatedBlog.setName("JavaCodingNerd");
bestJavaRelatedBlog.setUrl("http://javacodingnerd.com");
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;
@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("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}

 

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