In this tutorial, we're gonna build a Spring Boot Rest CRUD API example with Maven that use Spring Data JPA with H2 database. You'll know:
- Configure Spring Data, JPA, Hibernate to work with H2 Database
- Define Data Models and Repository interfaces
- Create Spring Rest Controller to HTTP requests
- Use Spring Data JPA
- PersonController - Responsible to receive all requests and provides all responses in JSON Format
- PersonRepository extends JPARepository - Responsible to make all database operations
- Person - Represents the model entity Person with your attributes.
- RestCrudSpringBootApplication - The main class of SpringBoot application
- application.properties - all database configurations
- To acess de database console - localhost:8080/h2-console
- JDBC URL: jdbc:h2:mem:restcrud
- User Name: admin
- Password: admin
mvn spring-boot:run
Download POSTMAN for JSON requests.
- Open your postman application in localhost:8080/api/persons
- METHOD: POST
- BODY: raw - type JSON
{
"name":"NAME",
"email": "NAME@gmail.com"
}
- RETURN
{
"id": 1,
"name": "NAME",
"email": "NAME@gmail.com",
"actived": false
}
- Open your postman application in localhost:8080/api/persons/{id}
- METHOD:PUT
- BODY: raw - type JSON
- ID: 1
{
"name":"NAME",
"email": "NAME@gmail.com",
"actived": true
}
- RETURN
{
"id": 1,
"name": "NAME",
"email": "NAME@gmail.com",
"actived": true
}
- Open your postman application in localhost:8080/api/persons/
- METHOD:GET
- BODY: EMPTY
- RETURN
[
{
"id": 1,
"name": "NAME",
"email": "NAME@gmail.com",
"actived": true
},
{
"id": 2,
"name": "NAME2",
"email": "NAME2@gmail.com",
"actived": false
},
{
"id": 3,
"name": "NAME3",
"email": "NAME3@gmail.com",
"actived": false
}
]
- Open your postman application in localhost:8080/api/persons/actived/
- METHOD:GET
- BODY: EMPTY
[
{
"id": 1,
"name": "NAME",
"email": "NAME@gmail.com",
"actived": true
}
]
-
Open your postman application in localhost:8080/api/persons?email={query}
-
METHOD:GET
-
BODY: EMPTY
-
email: 2
-
RETURN
[
{
"id": 2,
"name": "NAME2",
"email": "NAME2@gmail.com",
"actived": false
}
]
- Open your postman application in localhost:8080/api/persons/{id}
- METHOD:DELETE
- BODY: EMPTY
- RETURN: 204 No Content
- Open your postman application in localhost:8080/api/persons/
- METHOD:DELETE
- BODY: EMPTY
- RETURN: 204 No Content