This tutorial will walk you through the steps of building a full-stack CRUD web app and RESTful APIs web services example with Spring Boot, Lombok, JPA and Hibernate, MySQL, FreeMarker, VueJS and Axios
What you'll build
What you'll need
Your local computer should have JDK 8+ or OpenJDK 8+, Maven 3+, MySQL Server 5+ or Docker CE 18+
You should also walk through the following tutorials
Init project structure
You can create and init a new Spring Boot project by using Spring CLI or Spring Initializr. Learn more about using these tools here
The final project structure as below
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── hellokoding
│ │ └── springboot
│ │ └── restful
│ │ ├── product
│ │ │ ├── Product.java
│ │ │ ├── ProductAPI.java
│ │ │ ├── ProductController.java
│ │ │ ├── ProductRespository.java
│ │ │ └── ProductService.java
│ │ └── Application.java
│ └── resources
│ ├── static
│ │ ├── products.css
│ │ └── products.js
│ ├── templates
│ │ └── products.html
│ └── application.properties
├── Dockerfile
├── docker-compose.yml
└── pom.xml
Project dependencies
Create JPA Entity
@Data is a Lombok annotation which generates field getters and setters, toString, equals and hashCode methods for you at compile time
@Entity is a JPA annotation which specifies the class as an entity (so the class name can be used in JPQL queries) and as a table in the database (the @Entity class name will match with the underlying table name if the @Table annotation is omitted)
Learn more about JPA and Hibernate
Create Spring Data JPA Repository
Implement Service
RequiredArgsConstructor
is a Lombok annotation which generates a constructor with required fields (final fields and @NonNull fields). For the above ProductService class, Lombok will generate
@Service
public class ProductService {
private final ProductRespository productRespository;
public ProductService(ProductRespository productRespository) {
this.productRespository = productRespository;
}
...
}
For classes which only have single constructor, since Spring 4.3, you no longer need to specify an explicit injection annotation such as @Autowired, Spring does that for you
If your editor has not been installed Lombok plugin, you may get a highlighted error on the productRespository
field. Either compiling the project or installing the plugin will resolve the problem
Learn more about using Lombok in Java and Spring Boot
Create REST APIs
Create Web Controller
Create FreeMarker View Template
Static Files
Application Configurations
hk-mysql
refers to Docker Compose service defined in the below docker-compose.yml
file
spring.jpa.hibernate.ddl-auto=create
allows JPA/Hibernate auto create database and table schema for you
On production environment, you may like to disable the DDL Auto feature by using spring.jpa.hibernate.ddl-auto=validate or spring.jpa.hibernate.ddl-auto=none (default). Check out this example as one of the approaches Flyway Example of Database Migration/Evolution with Spring Boot, JPA and Hibernate
Run with Docker
Prepare Dockerfile for Java/Spring Boot application and docker-compose.yml
for MySQL Server
Type the below command at the project root directory, make sure your local Docker is running
docker-compose up
Run with your local MySQL Server
You can run the app with your local MySQL Server by updating hk-mysql
on application.properties to localhost
and type the below command at the project root directory
mvn clean spring-boot:run
Test the app
Access to localhost:8080
and start playing around with the app