Skip to content

Commit 200b2bd

Browse files
authored
Merge pull request #130 from WebFuzzing/spring-boot-restful-api-starter
starter of the spring-boot-restful-api-example
2 parents 5bc0abe + ee502b7 commit 200b2bd

36 files changed

+1729
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 S.M Lee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
[![HitCount](http://hits.dwyl.io/phantasmicmeans/spring-boot-restful-api-example.svg)](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+
![image](https://user-images.githubusercontent.com/28649770/44622337-69c67a80-a8f1-11e8-99d7-34adb90779a3.png)
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+
&nbsp;
58+
59+
60+
## RESTful API Server ##
61+
62+
&nbsp;
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+
&nbsp;
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+
&nbsp;
83+
84+
## Curl ##
85+
86+
&nbsp;
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+
&nbsp;
109+
110+
**2. Curl for Problem & SubProblem**
111+
&nbsp;
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+
&nbsp;
132+
133+
## Running the project with MySQL ##
134+
135+
append this at the end of application.yml
136+
&nbsp;
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+
&nbsp;
163+
164+
165+
## Swagger ##
166+
167+
You can use the Swagger API Documentation at http://{Your_Server}:{Port}/swagger-ui.html
168+
169+
![image](https://user-images.githubusercontent.com/28649770/44622453-8bc0fc80-a8f3-11e8-9223-b5a21717ba6d.png)
170+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>project-api</artifactId>
8+
<version>0.0.1</version>
9+
<packaging>jar</packaging>
10+
11+
<name>project-api</name>
12+
<description>project-api</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>3.0.4</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>17</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-validation</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.code.gson</groupId>
38+
<artifactId>gson</artifactId>
39+
<version>2.8.5</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springdoc</groupId>
43+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
44+
<version>2.0.2</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-data-jpa</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.mysql</groupId>
52+
<artifactId>mysql-connector-j</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-orm</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
76+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sw.project;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ProjectApiApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ProjectApiApplication.class, args);
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.sw.project.aspect;
2+
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
//@Aspect
8+
//@Component
9+
public class RestControllerAspect {
10+
11+
protected final Logger log = LoggerFactory.getLogger(this.getClass());
12+
13+
// @Pointcut("execution(* com.sw.project.controller.*(..)")
14+
// private void pointCutBeforeApi() {
15+
// }
16+
17+
//@Before("pointCutBeforeApi()")
18+
// public void loggerBeforeRestCall(JoinPoint joinPoint) throws Throwable{
19+
//
20+
// log.info("======== AOP Before RestAPI Call ==========" + joinPoint);
21+
// }
22+
//
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sw.project.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
5+
6+
/*
7+
* JPA Config
8+
*/
9+
@EnableJpaAuditing
10+
@Configuration
11+
public class JpaConfiguration {
12+
13+
}

0 commit comments

Comments
 (0)