manukempo / spring-boot-demo

A basic CRUD app using Spring Boot (v2.7.1 - Java 18)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring Boot Application

A Spring Boot app with Java 18.

Spring Boot Setup

Using Initializr

  1. Generate the boilerplate:

    • Spring Initializr
    • Basic Java-Maven project with Java 18 (Spring Web dependency is required) Basic project example
  2. 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):

    Set project SDK Set platform settings SDK

    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

  3. Set your IntelliJ to reload on save:

    • Add Spring Boot Devtools: Add Spring Boot DevTools

    • Reload the Maven dependencies (icon on the top right) Reload Maven dependencies

    • Allow auto-reload on server changes:

      • In Advanced Settings > Allow auto make... Allow to auto start
      • And tick also the Compiler Build project automatically Set project to build automatically

From Scratch

Spring Boot reference
Getting Started

  1. Install any missing local dependency like Maven. You can download the Binary zip archive and unzip it in your Program Files, and finally add a new environment variable under Path, e.g. C:\Program Files\apache-maven-3.8.6\bin. $ mvn -v

  2. 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 only com.example:myproject:jar:0.0.1-SNAPSHOT
    • Add the recommended dependencies and re-run the last command to install them
  3. Create the main Java file src/main/java/MyApplication.java with the SpringBoot syntax and run $ mvn spring-boot:run

  4. 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

RESTful API

  1. Create a package called Customer and a Spring Application (@SpringBootApplication):
    • Right button over java folder -> New > Package -> Customer
  • Right button over Customer package -> New > Java Class -> CustomerApplication. This must include the annotation @SpringBootApplication
  1. Create a Customer class with getters and setters for name and email

  2. Create a customer controller:

    • Right button over Customer package -> New > Java Class -> CustomerController. This must include the @RestController
  3. Use JPA (Java Persistence API) dependency: spring-boot-starter-data-jpa (search for it under Dependencies)

    • Install JPA dependency through IntelliJ Dependencies section spring-boot-starter-data-jpa
    • Add the annotation @Entity to the Customer class
    • Add a @PostMapping in the CustomerController, what will require a CustomerRepository interface <EntityType, EntityID>
    • Create a constructor in the CustomerController to inject the previous CustomerRepository 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 the Customer Entity for the id to provide the type Id and autogenerate a new one every time; Also do not forget to create a default Customer 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 called CustomerNotFoundException that extends RuntimeException with the customised message
      • Create a Controller Advice class under Customer package called CustomerNotFoundAdvice that includes the annotations @ControllerAdvice, @ResponseBody (only when there is a response), @ExceptionHandler(CustomerNotFoundException.class) (only when the CustomerNotFoundException is raised) and @ResponseStatus(HttpStatus.NOT_FOUND) (it will return a 404)
  4. 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 the resources directory under src/main

About

A basic CRUD app using Spring Boot (v2.7.1 - Java 18)


Languages

Language:Java 100.0%