Table of contents
No headings in the article.
No introductions , just quick pointers of what is what
of a simple spring boot application with two build tools.
Let's dive right into it.
Prerequisites
- Basic java knowledge, if there good else never mind you will pick it up on the way :)
- Install a good IDE , IntelliJ CE preferred, since it will help you to set everything up quickly
- JDK 11
- [optional] git bash in case you want to check out my sample repos with a sample rest endpoint
Thats all we are good to go.
So what is going to help us here to kickstart a new spring boot application is this beautiful ui spring provides us
You can pick and choose what type of project , packaging, java version etc. For the sake of this blog we are going to see both maven and gradle projects on java 11
Components
- Dependencies
maven pom.xml (for maven dummies, pom.xml is a holy grail of a maven project where you will define anything and everything about build , deploy , etc [literally any task you want to achieve] of a project)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
gradle build.gradle (much prettier counterpart of maven. pretty print officially likes gradle ๐)
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
- Spring Boot Application Create any public class and add below annotation
@SpringBootApplication
public class DemoApplication {
Lets see what this will do, remembering Phillip Webb, Stephane Nicoll, Andy Wilkinson (for them to have it explained kindly in the documentation)
Indicates a link to Configuration class that declares one or more @Bean
methods and also triggers @EnableAutoConfiguration
and @ComponentScan
component scanning. This is a convenience annotation that is equivalent to declaring @Configuration
, @EnableAutoConfiguration
and @ComponentScan
.
SpringApplication.run(DemoApplication.class, args);
Above code will run the spring application, creating and refreshing a new ApplicationContext which is returned.
Also here we have added rest controller in the main class itself as below. You can have your own controller class and annotate it with @RestController and define the endpoints there.
@RestController
@SpringBootApplication
public class DemoApplication {
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
Run DemoApplication and try accessing your endpoint with http://localhost:8080/hello/
. You will be able to see hello world as response in browser.
Congrats!! . You have successfully created a rest endpoint using spring boot.
Expert tip:
spring-boot-starter-actuator
dependency will additionally expose the actuator endpoint to monitor the health of your microservice. Try accessinghttp://localhost:8080/actuator/
Git links are provided below. Please feel free to clone and experiment.