- Starters,
- Auto-configuration,
- Beans,
- Actuator and more
Advantages
Spring Boot offers the following advantages to its developers −
- Easy to understand and develop spring applications
- Increases productivity
- Reduces the development time
Goals
Spring Boot is designed with the following goals −
- To avoid complex XML configuration in Spring
- To develop a production ready Spring applications in an easier way
- To reduce the development time and run the application independently
- Offer an easier way of getting started with the application
spring-boot-starter-actuator : monitor and manage your application.
spring-boot-starter-data-jpa : Spring and JPA for database access
spring-boot-starter-security : monitor and manage your application.
spring-boot-starter-web : is used to write a Rest Endpoints
spring-boot-starter-actuator : monitor and manage your application.
spring-boot-starter-thymeleaf : to create web application
spring-boot-starter-test : monitor and manage your application.
spring-boot-starter-web : Spring and JPA for database access
spring-boot-starter-parent
@SpringBootApplication : @EnableAutoConfiguration + @ComponentScan
@EnableAutoconfiguration : Automatically configures application based on the dependencies
Spring Initializer
CommandLineRunner
and ApplicationRunner
, to run specific pieces of code when an application is fully started. These interfaces get called just before run()
once SpringApplication
completes.Ordering
You can register as many application/command line runners as you want. You just need to register them as Beans in the application context. Then, Spring will automatically pick them up. You can order them as well either by extending interface org.springframework.core.Ordered
or via the @Order
annotation.
CommandLineRunner
and ApplicationRunner
is CommandLineRunner.run()
accepts String array[]
whereas ApplicationRunner.run()
accepts ApplicationArguments
as argument. you can find more information with example hereCommand Line Properties
@Value("${property_key_name}")
@Value("${spring.application.name:demoservice}")
SPRING RESTFUL DEPENDENCIES
<dependencies> <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> </dependency> </dependencies>
<artifactId>spring-boot-maven-plugin</artifactId>
ResponseEntity<Object>
ResponseEntity<Object> updateProduct(@PathVariable("id") String id)
return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
@RequestParam(value = "name", required = false, defaultValue = "honey") String name)
POST API
new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK);
return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);
package com.tutorialspoint.demo.exception; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class ProductExceptionController { @ExceptionHandler(value = ProductNotfoundException.class) public ResponseEntity<Object> exception(ProductNotfoundException exception) { return new ResponseEntity<>("Product not found", HttpStatus.NOT_FOUND); } }
ResponseEntity.ok(
"Hello World!"
);
new
ResponseEntity<>(
"Custom header set"
, headers, HttpStatus.OK)
return
ResponseEntity.ok()
.header(
"Custom-Header"
,
"foo"
)
.body(
"Custom header set"
);
23456@PostMapping
(value =
"/content"
, produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping
(value =
"/content"
, produces = MediaType.APPLICATION_XML_VALUE)
For monitoring and managing your microservice application via Spring Boot Admin Server, you should add the Spring Boot Admin starter client dependency and point out the Admin Server URI into the application properties file.
Note − For monitoring an application, you should enable the Spring Boot Actuator Endpoints for your Microservice application.
First, add the following Spring Boot Admin starter client dependency and Spring Boot starter actuator dependency in your build configuration file.