01 - Creating a Spring Boot project
springboot
web
REST
Getting started with Spring Boot
Introduction
Springboot is a Java framework that uses annotations to add functionalities to the SE packages
Spring boot project
Creating a new project
Go to the Spring initializr page to prepare a simple Maven project with the required dependencies:
- Spring Web
- Spring Boot DevTools
- Lombok
- Thymeleaf
- H2 Database
- Spring JDBC
- etc…
After that, click the Generate
button to download a Zip
file with the project structure.
Creating controllers
REST controller
Use the following annotations to create a REST controller:
HelloRestController.java
package com.protoss.myfirstspring.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloRestController {
@GetMapping("/hellorest")
public String HelloWorld() {
return "Hello World from the REST Controller!";
}
}
where:
@RestController
: this marks the class as the root of the REST controller@GetMapping("/${action}")
: maps the method to the given RESTaction
Web controller
Use the following annotations to create a Web controller:
HelloWebController.java
package com.protoss.myfirstspring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWebController {
// define handled path
@RequestMapping("/helloweb")
public String helloHandler() {
return "hello.html";
}
}
where:
@Controller
: this marks the class as the root of the Web controller@RequestMapping("/${page.html}")
: maps the method to the givenpage.html
located in thesrc/main/resources/templates
path