/
ExampleController.groovy
199 lines (190 loc) · 6.6 KB
/
ExampleController.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.groovy.swagger.groovyswaggerdemo
import com.groovy.swagger.groovyswaggerdemo.model.UserEntity
import groovy.util.logging.Slf4j
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.ArraySchema
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
@Slf4j
@Tag(
name = "Example controller",
description = "in this controller described documentation"
)
@RestController
class ExampleController {
@Operation(
summary = "Get user by path variable",
description = "return user entity"
)
@ApiResponses([
@ApiResponse(
responseCode = "200",
description = "Returns user entity",
content = [
@Content(
mediaType = "application/json",
schema = @Schema(implementation = UserEntity.class)
),
]
),
@ApiResponse(
responseCode = "404",
description = "User not found",
content = [@Content]
)
])
@GetMapping('/user/{name}')
UserEntity userNameFromPathVariable(
@Parameter(description = "You can find only user with name 'Adam'")
@PathVariable String name
) {
log.info("Get user name ${name} from path variable")
if (name.toLowerCase() == "adam") {
new UserEntity(
name: "Adam",
age: 21
)
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User with name ${name} not found!")
}
}
@Operation(
summary = "Get user by request param",
description = "return user entity"
)
@ApiResponses([
@ApiResponse(
responseCode = "200",
description = "Return user entity",
content = [
@Content(
mediaType = "application/json",
schema = @Schema(implementation = UserEntity.class)
)
]
),
@ApiResponse(
responseCode = "404",
description = "User not found",
content = [@Content]
)
])
@GetMapping('/user')
UserEntity userNameFromRequestParam(
@Parameter(description = "You can find only user with name 'Adam'")
@RequestParam String name
) {
log.info("Get user name ${name} from request param")
if (name.toLowerCase() == "adam") {
new UserEntity(
name: "Adam",
age: 21
)
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User with name ${name} not found!")
}
}
@Operation(
summary = "Get users list",
description = "return users list"
)
@ApiResponse(
responseCode = "200",
description = "Return users list",
content = [
@Content(
mediaType = "application/json",
array = @ArraySchema(
schema = @Schema(implementation = UserEntity.class)
)
)
]
)
@GetMapping('/user/list')
List<UserEntity> userNames() {
log.info("Get users list")
[new UserEntity(
name: "Adam",
age: 21
), new UserEntity(
name: "Jon",
age: 25
)]
}
@Operation(
summary = "Create user",
description = "create user entity"
)
@ApiResponse(responseCode = "200", description = "User created",
content = [
@Content(
mediaType = "application/json",
schema = @Schema(implementation = UserEntity.class)
)
]
)
@PutMapping('/user')
UserEntity addUser(
@RequestBody UserEntity userEntity
) {
log.info("User ${userEntity?.name} is created!")
userEntity
}
@Operation(
summary = "Save user",
description = "save user entity"
)
@ApiResponse(responseCode = "200", description = "User saved",
content = [
@Content(
mediaType = "application/json",
schema = @Schema(implementation = UserEntity.class)
)
]
)
@PostMapping('/user')
UserEntity saveUser(
@RequestBody UserEntity userEntity
) {
log.info("User ${userEntity?.name} is created!")
userEntity
}
@Operation(summary = "Delete user", description = "delete user entity")
@ApiResponses([
@ApiResponse(
responseCode = "200",
description = "User deleted",
content = [@Content]
),
@ApiResponse(
responseCode = "404",
description = "User not found",
content = [@Content]
)
])
@DeleteMapping('/user')
void deleteUserFromRequestParam(
@Parameter(description = "You can delete only user with name 'Adam'")
@RequestParam String name
) {
if (name.toLowerCase() == "adam") {
log.info("User with name ${name} is deleted!")
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User with name ${name} not found!")
}
}
}