Skip to content

johnazre/java-spring-api-checklist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to build a Java Spring API

Overview

This checklist will help you build a basic Spring API

Index

Steps

  1. Go to start.spring.io, set your metadata, and download a project with the following dependencies:
  • Spring Boot DevTools
  • Lombok
  • Spring Web
  • Spring Data JPA
  • The driver for whichever database you're using, i.e. PostgreSQL Driver, MySQL Driver, etc.
  1. It downloads a .zip file, so you will need to unzip it and put the contained folder wherever you store your projects on your hard drive, then open the folder with your code editor of choice.

  2. Within the main package, create a package for each entity that you will be using, i.e. contacts, todos, etc.

  3. Within each package that you create, it should have these 4 things:

    • Model Class (Contact, Todo, etc.)
    • Controller Class (ContactsController, TodosController, etc.)
    • Repository Interface (ContactsRepository, TodosRepository, etc.)
    • Service Class (ContactsService, TodosService, etc.)
  4. Within the Model class, you should have the following things:

    • The following annotations go above the class declaration
      • @Entity - To indicate that this an entity in the database
      • @Table (name = "whatever") - To indicate that a table should be created with the name of "whatever"
      • @Data - To add Lombok functionality. Lombok will automatically generate getters and setters for each property.
    • An empty constructor - We will talk about that later, but it has to do with serializing/deserializing JSON.
    • A private id field of type "long". Should have the following annotations:
      • @Id - To indicate that it is an "id" field
      • @GeneratedValue (strategy = GenerationType.IDENTITY) - To indicate that ids should be generated for new data
    • Private properties for the rest of the fields that this entity will have. These properties should have the following annotations:
      • @Column - To indicate that this should be a column in the table.
  5. Within the Repository interface, you should add that your repository extends "JpaRepository" and add a generic to the right of it that looks like <TheModelName, TheDataTypeOfTheIdField>.

  6. Within the Service class

  7. Within the Controller class:

    • The following annotations go above the class declaration
      • @RestController - Indicates that this will be a controller file
      • @RequestMapping("/contacts") - Maps this controller to a route prefix, such as "/contacts", in this case.
    • At the top of the controller class, add the @Autowired annotation and declare an instance of your service class.
    • Create a constructor that instantiates the injected service class.
    • Create all the route handler methods you need to manage your data, i.e. getAll, getOne, addOne, updateOne, removeOne, etc.
    • To indicate which HTTP method this route will look for, use one of the following annotations above the method declaration:
      • @GetMapping
      • @PostMapping
      • @PatchMapping
      • @PutMapping
      • @DeleteMapping
      • URL prefixes can be added to these methods like this: @GetMapping("/something")
      • If no prefix is added, it will default to a route on the root prefix of the controller
  8. Add the following code to the application.properties file:

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/your-database-name
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

NOTE: Make sure to change the username and password that corresponds to your database instance on your computer

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages