Spring Boot Application
A Spring Boot app with Java 18.
Spring Boot Setup
Using Initializr
-
Generate the boilerplate:
- Spring Initializr
- Basic Java-Maven project with Java 18 (
Spring Web
dependency is required)
-
Start a new project with the generated file and set
Project SDK
(e.g. 18 - if it is the latest Java version, you will need to download the JDK-18, and set all the project with it and the SDK with X-Experimental):NOTE: You can download the latest JDK version from Oracle and set the environment variable with the new Java path, e.g.
C:\Program Files\Java\jdk-18.0.1.1\bin
(remember to delete any existing Java path).$ java --version
-
Set your IntelliJ to reload on save:
From Scratch
Spring Boot reference
Getting Started
-
Install any missing local dependency like Maven. You can download the
Binary zip archive
and unzip it in yourProgram Files
, and finally add a new environment variable underPath
, e.g.C:\Program Files\apache-maven-3.8.6\bin
.$ mvn -v
-
At this point, you should have only the
.gitignore
in your project folder.- Copy the basic
pom.xml
from the reference - Run:
$ mvn package
to detects the Maven and gives you a working build$ mvn dependency:tree
to check the pom file and install any dependency, what it is onlycom.example:myproject:jar:0.0.1-SNAPSHOT
- Add the recommended dependencies and re-run the last command to install them
- Copy the basic
-
Create the main Java file
src/main/java/MyApplication.java
with the SpringBoot syntax and run$ mvn spring-boot:run
-
Create a completely self-contained executable Jar that we could run in production
- Add the required plugins to the
pom.xml
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- Click again the Maven button; run
$ mvn package
; and finally$ java -jar ./target/myproject-0.0.1-SNAPSHOT.jar
- Add the required plugins to the
RESTful API
- Create a package called
Customer
and a Spring Application (@SpringBootApplication
):- Right button over
java
folder -> New > Package ->Customer
- Right button over
- Right button over
Customer
package -> New > Java Class ->CustomerApplication
. This must include the annotation@SpringBootApplication
-
Create a
Customer
class with getters and setters forname
andemail
-
Create a customer controller:
- Right button over
Customer
package -> New > Java Class ->CustomerController
. This must include the@RestController
- Right button over
-
Use JPA (Java Persistence API) dependency:
spring-boot-starter-data-jpa
(search for it underDependencies
)- Install JPA dependency through IntelliJ Dependencies section
spring-boot-starter-data-jpa
- Add the annotation
@Entity
to theCustomer
class - Add a
@PostMapping
in theCustomerController
, what will require aCustomerRepository
interface<EntityType, EntityID>
- Create a constructor in the
CustomerController
to inject the previousCustomerRepository
to be able to use all the available methods (dependency injection
) like.save()
- Add a DB dependency in the
pom.xml
file, e.g.H2
can be found using the initializr: ADD DEPENDENCIES > H2 Database
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
- Add the annotations
@Id @GeneratedValue
in theCustomer Entity
for theid
to provide the typeId
and autogenerate a new one every time; Also do not forget to create a defaultCustomer
constructor for the same entity - POST request example:
{ "name": "Manu", "email": "manukempo@gmail.com" }
- Handle exceptions 404:
- Create a Handler Exception class under
Customer
package calledCustomerNotFoundException
that extendsRuntimeException
with the customised message - Create a Controller Advice class under
Customer
package calledCustomerNotFoundAdvice
that includes the annotations@ControllerAdvice
,@ResponseBody
(only when there is a response),@ExceptionHandler(CustomerNotFoundException.class)
(only when theCustomerNotFoundException
is raised) and@ResponseStatus(HttpStatus.NOT_FOUND)
(it will return a404
)
- Create a Handler Exception class under
- Install JPA dependency through IntelliJ Dependencies section
-
Connect with a MySQL database
- Add MySQL dependency, what can be found through the initializr: ADD DEPENDENCIES > MySQL Driver, to the
pom.xml
file - Specify the DB details in the
application.properties
under theresources
directory undersrc/main
- Add MySQL dependency, what can be found through the initializr: ADD DEPENDENCIES > MySQL Driver, to the