Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Spring Boot 2.0

You're reading from   Mastering Spring Boot 2.0 Build modern, cloud-native, and distributed systems using Spring Boot

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781787127562
Length 390 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dinesh Rajput Dinesh Rajput
Author Profile Icon Dinesh Rajput
Dinesh Rajput
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with Spring Boot 2.0 2. Customizing Auto-Configuration in Spring Boot Application FREE CHAPTER 3. Getting Started with Spring CLI and Actuator 4. Getting Started with Spring Cloud and Configuration 5. Spring Cloud Netflix and Service Discovery 6. Building Spring Boot RESTful Microservice 7. Creating API Gateway with Netflix Zuul Proxy 8. Simplify HTTP API with Feign Client 9. Building Event-Driven and Asynchronous Reactive Systems 10. Building Resilient Systems Using Hystrix and Turbine 11. Testing Spring Boot Application 12. Containerizing Microservice 13. API Management 14. Deploying in Cloud (AWS) 15. Production Ready Service Monitoring and Best Practices 16. Other Books You May Enjoy

Implementing the REST service

Let's start by creating a simple REST controller as follows:

package com.dineshonjava.masteringspringboot.controller;

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

@RestController
public class HelloController {

   @GetMapping("/hello")
   String sayHello(){
         return "Hello World!!!";
   }
} 

Let's see this small REST controller (HelloController) in detail:

  • @RestController annotation: It indicates that this is the controller class and its result writes into the response body and doesn't want to render view
  • @GetMapping annotation: It indicates a request handler method and it is a shorthand annotation for @RequestMapping(method = RequestMethod.GET)
  • sayHello() method: It returns a greeting message

In the STS IDE, you could run your application as a Spring Boot application with an embedded server by selecting Run As | Spring Boot Application from the Run menu as follows:

Spring Initialzr creates the main application launcher class as follows:

package com.dineshonjava.masteringspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MasteringSpringBootApplication {

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

This tiny class is actually a fully operational web application! Let's see some details:

  • @SpringBootApplication: This annotation tells Spring Boot, when launched, to scan recursively for Spring components inside this package and register them. It also tells Spring Boot to enable auto-configuration, a process where beans are automatically created based on classpath settings, property settings, and other factors.
  • main() method: It is a simple public static void main() method to run the application.
  • SpringApplication.run(): The SpringApplication class is responsible for creating the Spring application's context, and the run() method initializes the application's context in your Spring application.

Let's run your Spring Boot application and observe the logs on the console as follows:

As you can see in the console logs, you can observe several things:

  • Logs have the Spring Boot banner at the top of the logs and Spring Boot version. You can also add your own ASCII banner by creating banner.txt or banner.png and putting it into the src/main/resources/ folder.
  • There is an embedded Tomcat server with the server port 8080; it is the default port, but you can customize it by adding the server.port property to the application.properties file as follows:

server.port= 8181

  • Logs also shows all possible request mappings of your application as follows:

As you can see, your application is running on the default embedded Tomcat server with the default server port, 8080. Let's verify it on the system browser, where it will look as follows:

In this chapter, we have created a very simple Hello World REST application and run this application on the embedded Tomcat server of Spring Boot.

Let's see what new features and enhancements have been added to the new version of Spring Boot, 2.0.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image