|
1 | 1 | # fast-crud |
2 | | -Automatic creation of simple CRUD API of Spring boot and JPA project. |
| 2 | +매번 반복되는 Spring boot & JPA 의 기본적인 CRUD API 를 자동화 할 수 있지 않을까? |
3 | 3 |
|
4 | | -<br> |
| 4 | +latest version : 1.0.1 |
5 | 5 |
|
6 | | -## How to use |
| 6 | +## 기능들 |
| 7 | +- 어노테이션 표시로 엔티티의 Create, Read, Update, Delete API 가 생성된다. |
| 8 | +- 자동 생성된 API의 Root path 를 설정할 수 있다. |
| 9 | +- CRUD 중 추가할, 또는 제외할 기능을 선택할 수 있다. |
7 | 10 |
|
8 | | -#### Step 1. Add the dependency. |
| 11 | +## 미리보기 |
| 12 | +``` java |
| 13 | +@CRUD(repositoryType = SampleRepository.class) |
| 14 | +@Entity |
| 15 | +class Sample { |
| 16 | +} |
| 17 | +``` |
9 | 18 |
|
10 | | -<details> |
11 | | -<summary>Gradle (build.gradle)</summary> |
| 19 | +Entity 의 @CRUD 를 읽어, 아래의 API가 자동 생성된다. |
12 | 20 |
|
13 | | - ``` groovy |
| 21 | +``` |
| 22 | +[POST] /sample |
| 23 | +[GET] /sample |
| 24 | +[GET] /sample/{id} |
| 25 | +[PUT] /sample |
| 26 | +[DELETE] /sample/{id} |
| 27 | +``` |
| 28 | + |
| 29 | +## 사용 방법 |
| 30 | + |
| 31 | +### build.gradle |
| 32 | + |
| 33 | +라이브러리 의존성을 추가한다. |
| 34 | +``` |
14 | 35 | repositories { |
15 | 36 | maven { url 'https://jitpack.io' } |
16 | 37 | } |
17 | | -``` |
18 | 38 |
|
19 | | - ``` groovy |
20 | 39 | dependencies { |
21 | | - implementation 'com.github.ecsimsw:fast-crud:0.0.2' |
| 40 | + implementation 'com.github.ecsimsw:api-shutdown:1.0.1' |
22 | 41 | } |
23 | 42 | ``` |
24 | | -</details> |
25 | 43 |
|
26 | | -<details> |
27 | | -<summary>Maven (pom.xml)</summary> |
28 | | - |
29 | | -``` xml |
30 | | -<repositories> |
31 | | - <repository> |
32 | | - <id>jitpack.io</id> |
33 | | - <url>https://jitpack.io</url> |
34 | | - </repository> |
35 | | -</repositories> |
36 | | -``` |
37 | | -``` xml |
38 | | -<dependency> |
39 | | - <groupId>com.github.ecsimsw</groupId> |
40 | | - <artifactId>fast-crud</artifactId> |
41 | | - <version>0.0.2</version> |
42 | | -</dependency> |
43 | | -``` |
| 44 | +### @EnableCrud 추가 |
| 45 | + |
| 46 | +@EnableCrud 로 라이브러리 사용을 활성화한다. |
44 | 47 |
|
45 | | -</details> |
| 48 | +``` java |
| 49 | +@EnableCrud |
| 50 | +@SpringBootApplication |
| 51 | +public class MyApplication {} |
| 52 | +``` |
46 | 53 |
|
47 | | -<br> |
| 54 | +### @CRUD 표시 |
48 | 55 |
|
49 | | -#### Step 2. Put @CRUD on your entity class. |
| 56 | +CRUD 를 적용할 Entity에 @CRUD와 엔티티 Repository type 를 표시한다. |
50 | 57 |
|
51 | 58 | ``` java |
52 | | -@CRUD |
| 59 | +@CRUD(repositoryType = SampleRepository.class) |
53 | 60 | @Enity |
54 | 61 | class Sample { |
55 | 62 | } |
56 | 63 | ``` |
57 | 64 |
|
58 | | -<br> |
59 | | - |
60 | | -#### Step 3. Declare JpaRepository with entity name. |
61 | | - |
62 | 65 | ``` java |
63 | | -public interface SampleRepository extends JpaRepository<Sample, Long> {} |
| 66 | +interface SampleRepository extends JpaRepository<Sample, Long> { |
| 67 | +} |
64 | 68 | ``` |
65 | 69 |
|
66 | | -<br> |
| 70 | +### API 명세 |
67 | 71 |
|
68 | | -#### Step 4. That's it. You just made basic CRUD http api bellow. |
| 72 | +아래 CRUD api 가 자동 생성된다. |
| 73 | +생성과 수정의 Request body 에는 Entity 의 프로퍼티 키와 값이 포함된다. |
69 | 74 |
|
70 | | -| |HttpMethod|Path|RequestBody (Json)| |
71 | | -|----|------|----|-----| |
72 | | -|save|POST|/{entityName}|O| |
73 | | -|findAll|GET|/{entityName}|X| |
74 | | -|findById|GET|/{entityName}/{id}|X| |
75 | | -|update|PUT|/{entityName}/{id}|O| |
76 | | -|delete|DELETE|/{entityName}/{id}|X| |
| 75 | +#### 생성 |
| 76 | +``` |
| 77 | +[POST] /sample |
| 78 | +{ |
| 79 | + "name" : "ecsimsw" |
| 80 | +} |
| 81 | +``` |
77 | 82 |
|
78 | | -<br> |
| 83 | +#### 조회 |
79 | 84 |
|
80 | | -#### Step 5. Example |
81 | | -``` java |
82 | | -@CRUD |
83 | | -@Entity |
84 | | -public class Sample { |
85 | | - |
86 | | - @GeneratedValue |
87 | | - @Id |
88 | | - private Long id; |
89 | | - private String name; |
90 | | - |
91 | | - public Sample() { |
92 | | - } |
93 | | - |
94 | | - public Sample(String name) { |
95 | | - this.name = name; |
96 | | - } |
97 | | - |
98 | | - public Long getId() { |
99 | | - return id; |
100 | | - } |
101 | | - |
102 | | - public String getName() { |
103 | | - return name; |
104 | | - } |
105 | | -} |
| 85 | +``` |
| 86 | +[GET] /sample/{id} |
106 | 87 | ``` |
107 | 88 |
|
108 | | -``` java |
109 | | -public interface SampleRepository extends JpaRepository<Sample, Long> {} |
| 89 | +#### 전체 조회 |
| 90 | +``` |
| 91 | +[GET] /sample |
110 | 92 | ``` |
111 | 93 |
|
112 | | -``` |
113 | | -[POST] localhost:8080/sample |
114 | | -{ |
115 | | - "name" : "ecsimsw" |
116 | | -} |
117 | | -[PUT] localhost:8080/sample/1 |
| 94 | +#### 수정 |
| 95 | + |
| 96 | +``` |
| 97 | +[PUT] /sample/{id} |
118 | 98 | { |
119 | | - "name" : "new_name" |
| 99 | + "name" : "new_name" |
120 | 100 | } |
121 | | -[GET] localhost:8080/sample |
122 | | -[GET] localhost:8080/sample/1 |
123 | | -[DELETE] localhost:8080/sample/1 |
124 | 101 | ``` |
125 | 102 |
|
126 | | -<br> |
127 | | - |
128 | | -## Additional features |
129 | | - |
130 | | -#### Repository name |
131 | | - |
132 | | -You can set your repository bean name in @CRUD with `repositoryBean` parameter. |
| 103 | +#### 삭제 |
133 | 104 |
|
134 | | -``` java |
135 | | -@CRUD(repositoryBean = "anotherName") |
136 | 105 | ``` |
137 | | - |
138 | | -``` java |
139 | | -public interface AnotherName extends JpaRepository<Sample, Long> {} |
| 106 | +[DELETE] /sample/{id} |
140 | 107 | ``` |
141 | 108 |
|
142 | | -<br> |
| 109 | +### Root path 설정 |
143 | 110 |
|
144 | | -#### API root path |
145 | | - |
146 | | -You can set your api `root path` |
| 111 | +기본 API 기본 경로는 Entity 이름이다. @CRUD 어노테이션의 rootPath 값으로 API 기본 경로를 변경할 수 있다. |
| 112 | +아래 예시의 경우 `/api/sample` 으로 API 경로가 생성된다. |
147 | 113 |
|
148 | 114 | ``` java |
149 | | -@CRUD(rootPath = "anotherRoot") |
| 115 | +@CRUD(rootPath = "api/sample") |
| 116 | +@Entity |
| 117 | +class Sample {} |
150 | 118 | ``` |
151 | 119 |
|
| 120 | +### 제외 기능 설정 |
| 121 | + |
| 122 | +CRUD 중 제외할 기능을 명시할 수 있다. |
| 123 | +아래 예시의 겨우 엔티티의 수정과 삭제 기능없이, 생성과 조회 API 만 자동 생성된다. |
| 124 | + |
152 | 125 | ``` |
153 | | -/anotherRoot |
154 | | -/anotherRoot/{id} |
| 126 | +@CRUD(excludeType = {CrudType.DELETE, CrudType.UPDATE}) |
| 127 | +@Entity |
| 128 | +class Sample { |
155 | 129 | ``` |
156 | 130 |
|
157 | | -<br> |
| 131 | +## 변경 사항 |
158 | 132 |
|
159 | | -#### Exclude method |
| 133 | +### v1.0.0 |
160 | 134 |
|
161 | | -Method can be excluded in @CRUD with `exclude` parameter. |
| 135 | +Spring boot 2.6 이상부터 MVC 핸들러 매핑 경로 매칭 기본 전략이 AntPathMatcher -> PathPatternParser 로 수정되었다. RequestMappingHandlerMapping 에 추가되는 RequestMappingInfo 에 PathMatcher 를 설정하는 것으로, 기본 전략은 유지한 채 라이브러리에서 자동 생성하는 RequestMapping 정보만 AntPathMatcher를 따르도록 수정한다. |
162 | 136 |
|
163 | 137 | ``` java |
164 | | -@CRUD(exclude = {CrudMethod.UPDATE, CrudMethod.DELETE}) |
| 138 | +public HandlerInfo(CrudRequestHandler handlerInstance, RequestMethod httpMethod, String requestPath) { |
| 139 | + this.handler = handlerInstance; |
| 140 | + var buildConfig = new RequestMappingInfo.BuilderConfiguration(); |
| 141 | + buildConfig.setPathMatcher(new AntPathMatcher()); |
| 142 | + buildConfig.setPatternParser(new PathPatternParser()); |
| 143 | + this.requestMappingInfo = RequestMappingInfo |
| 144 | + .paths(requestPath) |
| 145 | + .methods(httpMethod) |
| 146 | + .options(buildConfig) |
| 147 | + .build(); |
| 148 | +} |
165 | 149 | ``` |
| 150 | + |
| 151 | + |
0 commit comments