A REST API built on Java, Spring Boot and H2, PostgreSQL or MySQL Server database.
Endpoint for requests:
https://odemur-spring-boot-rest-api.herokuapp.com/customer
URL Swagger UI:
https://odemur-spring-boot-rest-api.herokuapp.com/swagger-ui/index.html
- Language: Java
- Framework: Spring Boot
- Build Automation Tool: Apache Maven
- Database [default]: H2
- Supported Database: PostgreSQL and MySQL Server
- Service API: REST
Clone this project and running on you local machine.
Only for development and testing purposes.
- Java SDK 8
- Java IDE
- Apache Maven (only for Command Line)
Please follow carefully step by step instructions below:
- For PostgreSQL:
docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=123 -d postgres
docker exec -it postgres psql -U postgres
- For MySQL:
docker run --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 -d mysql/mysql-server:5.7
docker exec -it mysql mysql -uroot -p
Please create a database with the name: customer
CREATE DATABASE customer;
git clone https://github.com/odemur/spring-boot-rest-api.git
- Add PostgreSQL dependencies on file pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
- Edit the file "application.properties" on the directory "src/main/resorces".
Parameters for PostgreSQL
spring.datasource.url = jdbc:postgresql://localhost:5432/customer
spring.datasource.username = postgres
spring.datasource.password = 123
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
- Add MySQL dependencies on file pom.xml
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
- Edit the file "application.properties" on the directory "src/main/resorces".
Parameters for MySQL
spring.datasource.url = jdbc:mysql://localhost:3306/customer
spring.datasource.username = root
spring.datasource.password = 123
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=customer
spring.datasource.username=your_sql_server_username
spring.datasource.password=your_sql_server_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
- Open this project folder with any Java IDE (IntelliJ IDEA, Eclipse, etc..)
- Run the application using the IDE > Run Option
- Open Terminal
- Change directory to the base project (spring-boot-rest-api)
- Run the command
mvn spring-boot-rest-api:run
To test API Endpoint is necessary an HTTP Client Tool.
I recommed the app Postman [https://www.postman.com/downloads]
Create new customer from JSON structure.
-
Endpoint
/customer
-
Method
POST
-
Body Data (application/json)
{ "firstName": "John", "lastName": "Doe", "email": "[email protected]" }
-
Response
-
Code: 201
Content:{ "id": 1, "firstName": "John", "lastName": "Doe", "email": "[email protected]" }
-
Returns JSON list of all customers data.
-
Endpoint
/customer
-
Method
GET
-
Response
-
Code: 200
Content:
[{ "id": 1, "firstName": "John", "lastName": "Doe", "email": "[email protected]" }, { "..." }]
-
Returns JSON of a specific customer by ID.
-
Endpoint
/customer/:id
-
URL Params
Required
id=[integer]
-
Method
GET
-
Response
-
Code: 200
Content:
{ "id": 1, "firstName": "John", "lastName": "Doe", "email": "[email protected]" }
-
Updates customer with the values sent as JSON.
Returns an updated JSON structure.
-
Endpoint
/customer/:id
-
Method
PUT
-
URL Params
Required:
id=[integer]
-
Body Data (application/json)
{ "firstName": "Mary", "lastName": "Jane", "email": "[email protected]" }
-
Success Response
-
Code: 200
Content:
{ "id": 1, "firstName": "Mary", "lastName": "Jane", "email": "[email protected]" }
-
Delete customer by ID.
-
Endpoint
/customer/:id
-
Method:
DELETE
-
URL Params
Required:
id=[integer]
-
Success Response:
- Code: 200
- Code: 200