Q1. What is the key benefit of using Spring boot?
A1. The key benefit is that you can “build a production ready application from scratch in a matter of minutes”.
Over the years since its inception, Spring has grown to be very complex in terms of the amount of configuration an application requires. This is where Spring Boot comes in handy to simplify the configuration with opinionated defaults & inclusion of the libraries for you to get started quickly.
1) The Spring jars dependency management and versioning are simplified as demonstrated below with https://start.spring.io/.
Spring Boot’s main benefit is its ability to configure resources based on what it finds in your classpath. If your Maven POM includes JPA dependencies and a PostgreSQL driver, then Spring Boot will setup a persistence unit based on PostgreSQL. If you’ve added a web dependency, then you get Spring MVC configured with sensible defaults.
2) Spring boot is based on an HTTP server. Spring Boot has an embedded version of Tomcat by default, but gives you a way to opt for Jetty server if you wish.
Step 1: Go to https://start.spring.io/ to create a skeleton spring-boot application. Add “Spring Web“, “Spring Boot Actuator“, “Spring HATEOAS” and “Spring REST Docs“.
Generate & download the project skeleton artefacts by clicking on the “Generate” button. Copy the “simple-rest-api.zip” to the projects folder and unzip it. Open this folder via “Visual Studio Code” editor.
Step 2: The pom.xml file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>simple-rest-api</artifactId> <version>0.0.1-SNAPSHOT</version> <name>simple-rest-api</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-hateoas</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.restdocs</groupId> <artifactId>spring-restdocs-mockmvc</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.asciidoctor</groupId> <artifactId>asciidoctor-maven-plugin</artifactId> <version>1.5.8</version> <executions> <execution> <id>generate-docs</id> <phase>prepare-package</phase> <goals> <goal>process-asciidoc</goal> </goals> <configuration> <backend>html</backend> <doctype>book</doctype> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.springframework.restdocs</groupId> <artifactId>spring-restdocs-asciidoctor</artifactId> <version>${spring-restdocs.version}</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Step 3: Create a REST service. Create a new folder “controller” under “com.example.simplerestapi” in “src/main/java“.
HelloController.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.example.simplerestapi.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello from simple REST API"; } } |
SimpleRestApiApplication.java in “com.example.simplerestapi” in “src/main/java“.
SimpleRestApiApplication.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.example.simplerestapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SimpleRestApiApplication { public static void main(String[] args) { SpringApplication.run(SimpleRestApiApplication.class, args); } } |
Step 4: Open a new terminal window. Build & run .
|
1 2 |
~/projects/simple-rest-api]$ mvn clean package |
This builds: ~projects/simple-rest-api/target/simple-rest-api-0.0.1-SNAPSHOT.jar
|
1 2 |
~/projects/simple-rest-api]$ java -jar target/simple-rest-api-0.0.1-SNAPSHOT.jar |
Step 5: Open a new browser window and type
“http://localhost:8080/hello”
Outputs: hello from simple REST API
Changing the port from default 8080 to 8888
ctrl+c to kill the service from the terminal window.
Step 6: Open “application.properties” in “src/main/resources” folder and add the property “server.port”.
application.properties
|
1 2 |
server.port=8888 |
Step 7: Build & run.
|
1 2 |
~/projects/simple-rest-api]$ mvn clean package |
This builds: ~projects/simple-rest-api/target/simple-rest-api-0.0.1-SNAPSHOT.jar
|
1 2 |
~/projects/simple-rest-api]$ java -jar target/simple-rest-api-0.0.1-SNAPSHOT.jar |
Step 8: Open a new browser window and type
“http://localhost:8888/hello”
Outputs: hello from simple REST API
Spring Boot Actuator for monitoring
URL: http://localhost:8888/actuator
Outputs:
|
1 2 |
{"_links":{"self":{"href":"http://localhost:8888/actuator","templated":false},"health":{"href":"http://localhost:8888/actuator/health","templated":false},"health-path":{"href":"http://localhost:8888/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8888/actuator/info","templated":false}}} |
Now try:
URL: http://localhost:8888/actuator/health
Outputs:
|
1 2 3 |
{"status":"UP"} |
