|
| 1 | +[](http://hits.dwyl.io/phantasmicmeans/spring-boot-restful-api-example) |
| 2 | + |
| 3 | +# Spring Boot RESTful API - JPA Hibernate MySQL Example # |
| 4 | +*by S.M.Lee(phantasmicmeans)* |
| 5 | + |
| 6 | +RESTful API using Spring Boot, Swagger2, JPA hibernate and Mysql, One to Many, Many to One bidirectional mapping |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Relation ## |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +### Bidirectional Mapping ### |
| 16 | + |
| 17 | +* Project - Problem (One-To-Many) |
| 18 | +* Problem - Project (Many-To-One) |
| 19 | + |
| 20 | +* Problem - SubProblem (One-To-Many) |
| 21 | +* SubProblem - Problem (Many-To-One) |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Before we go, Check the domain class ## |
| 26 | + |
| 27 | + **1. Problem.java(part of)** |
| 28 | + |
| 29 | +```java |
| 30 | +@OneToMany(mappedBy = "project", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, orphanRemoval = true) |
| 31 | +private Set <Problem> problems = new HashSet<>(); |
| 32 | +/* Project <-> Problem One to Many bidirectional */ |
| 33 | +``` |
| 34 | + |
| 35 | +**2. Problem.java(part of)** |
| 36 | + |
| 37 | +```java |
| 38 | +@ManyToOne(cascade = CascadeType.REMOVE) |
| 39 | +@JoinColumn(name = "code", referencedColumnName = "code", nullable = false) |
| 40 | +private Project project; |
| 41 | +/* Problem <-> Project Many to One bidirectional */ |
| 42 | + |
| 43 | +@OneToMany(mappedBy = "problem", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) |
| 44 | +private Set<subProblem> subProblems = new HashSet<>(); |
| 45 | +//Problem <-> SubProblem One to Many bidirectional |
| 46 | +``` |
| 47 | + |
| 48 | +**3. SubProblem.java(part of)** |
| 49 | + |
| 50 | +```java |
| 51 | +@ManyToOne(cascade = CascadeType.REMOVE) |
| 52 | +@JoinColumn(name = "pro_idx", referencedColumnName = "idx", nullable = false) |
| 53 | +private Problem problem; |
| 54 | +/* Problem <-> Project Many to One bidirectional */ |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## RESTful API Server ## |
| 61 | + |
| 62 | + |
| 63 | +**1. API Description for Project** |
| 64 | + |
| 65 | +METHOD | PATH | DESCRIPTION |
| 66 | +------------|-----|------------ |
| 67 | +GET | /api/project/{code} | get Project-Problem-SubProblem with code |
| 68 | +POST | /api/project | save Project (code will generate by constructor) |
| 69 | +DELETE | /api/project/{code} | delete Project with code |
| 70 | +PUT | /api/project/{code} | update Project with code |
| 71 | + |
| 72 | + |
| 73 | +**2. API Description for Problem & SubProblem** |
| 74 | + |
| 75 | +METHOD | PATH | DESCRIPTION |
| 76 | +------------|-----|------------ |
| 77 | +GET | /api/problem/{code} | get all Problem-Subproblem with code |
| 78 | +POST | /api/problem/{code} | save Problem with code |
| 79 | +DELETE | /api/problem/{code}/all | delete all Problem-Subproblem with code |
| 80 | +POST | /api/subproblem | save Subproblem |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +## Curl ## |
| 85 | + |
| 86 | + |
| 87 | +**1. Curl for Project** |
| 88 | + |
| 89 | +1. Get a Project with code |
| 90 | +```bash |
| 91 | +curl -X GET http://localhost:8080/problem/0gn547 |
| 92 | +``` |
| 93 | + |
| 94 | +2. Save a Project with code |
| 95 | +```bash |
| 96 | +curl -d '{"title":"first project"}' -H "Content-Type: application/json" -X POST http://localhost:8080/project |
| 97 | +``` |
| 98 | + |
| 99 | +3. Delete a Project with code |
| 100 | +```bash |
| 101 | +curl -X DELETE http://localhost:8001/project/0gn547 |
| 102 | +``` |
| 103 | + |
| 104 | +4. Update a Project with code |
| 105 | +```bash |
| 106 | +curl -X PUT -H "Content-Type: application/json; charset=utf-8" -d '{"title":"first-project-renewal"}' http://localhost:8080/project/hx6029 |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +**2. Curl for Problem & SubProblem** |
| 111 | + |
| 112 | + |
| 113 | +1. Get a Problem with code |
| 114 | +```bash |
| 115 | +curl -X GET http://localhost:8001/problem/0gn547 |
| 116 | +``` |
| 117 | + |
| 118 | +2. Save a Problem with code |
| 119 | +```bash |
| 120 | +curl -d '{"title":"first problem"}' -H "Content-Type: application/json" -X POST http://localhost:8080/problem/hx6029 |
| 121 | +``` |
| 122 | + |
| 123 | +3. Delete a Problem-SubProblem with code |
| 124 | +```bash |
| 125 | +curl -X DELETE http://localhost:8001/problem/hx6029/all |
| 126 | +``` |
| 127 | +4. Save a SubProblem |
| 128 | +```bash |
| 129 | +curl -d '{"content":"first-subproblem","pro_idx":1}' -H "Content-Type: application/json" -X POST http://localhost:8080/subproblem |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +## Running the project with MySQL ## |
| 134 | + |
| 135 | +append this at the end of application.yml |
| 136 | + |
| 137 | + |
| 138 | +```yml |
| 139 | +spring: |
| 140 | + application: |
| 141 | + name: project-api |
| 142 | + |
| 143 | +## Hibernate Properties |
| 144 | +# The SQL dialect makes Hibernate generate better SQL for the chosen database |
| 145 | + jpa: |
| 146 | + properties: |
| 147 | + hibernate: |
| 148 | + dialect: org.hibernate.dialect.MySQL5InnoDBDialect |
| 149 | + hibernate: |
| 150 | + ddl-auto: update |
| 151 | + # Hibernate ddl auto (create, create-drop, validate, update) |
| 152 | + |
| 153 | + datasource: |
| 154 | + url: jdbc:mysql://{YOUR_MSQL_SERVER}:3306/{DATABASE NAME}?useSSL=false |
| 155 | + username: {YOUR_MYSQL_ID} |
| 156 | + password: {YOUR_MYSQL{PASSWORD} |
| 157 | + driver-class-name: com.mysql.jdbc.Driver |
| 158 | + hikari: |
| 159 | + maximum-pool-size: 2 |
| 160 | +``` |
| 161 | +
|
| 162 | + |
| 163 | +
|
| 164 | +
|
| 165 | +## Swagger ## |
| 166 | +
|
| 167 | +You can use the Swagger API Documentation at http://{Your_Server}:{Port}/swagger-ui.html |
| 168 | +
|
| 169 | + |
| 170 | +
|
0 commit comments