Wednesday, July 14, 2021

Swager UI for SpringBoot

 Swagger UI is a collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from an OAS-compliant API. This means if we have an Springboot application with the help of plug and play method we can create the documentation of our Spring boot application as show below.

For more details you can visit to the below url

https://swagger.io/tools/swagger-ui/

Please follow the below steps religiously

1- Create a springboot project with just springweb as dependecies using spring starter. I am using STS ide.
2- Add following dependencies in the pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
            <scope>compile</scope>
        </dependency>

3- SimpleSpringbootSwaggerApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.siddhu;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SimpleSpringbootSwaggerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SimpleSpringbootSwaggerApplication.class, args);
    }
 
     
 
}

4- SwaggerConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.siddhu.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .build();
              
    }
     
}

5- IndexController

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
package com.siddhu.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.siddhu.model.Employee;
 
import io.swagger.annotations.Api;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@RestController
public class IndexController {
    @GetMapping("/getEmployee")
    public String getEmployee() {
        Employee objEmployee =  new Employee("1","Siddhu");
        return "getEmployee called" + objEmployee.toString();
    }
 
 
    @PostMapping("/insertEmployee/{id}/{name}")
    public String insertEmployee(@PathVariable("id") String id ,@PathVariable("name") String name) {
        Employee objEmployee =  new Employee(id,name);
        System.out.println("insertEmployee called for id"+ objEmployee.getId() +"for name:" + objEmployee.getName());
        return "insertEmployee called" + objEmployee.toString();
 
    }
 
    @PutMapping("/updateEmployee/{id}/{name}")
    public String updateEmployee(@PathVariable("id") String id ,@PathVariable("name") String name) {
        Employee objEmployee =  new Employee(id,name);
        System.out.println("updateEmployee called for id"+ objEmployee.getId() +"for name:" + objEmployee.getName());  
        return "updateEmployee called" + objEmployee.toString();
 
    }
 
    @DeleteMapping("/deleteEmployee/{id}")
    public String deleteEmployee(@PathVariable("id") String id) {
        Employee objEmployee =  new Employee(id);
        System.out.println("deleteEmployee called for id"+ objEmployee.getId()+"for name:" + objEmployee.getName());
        return "deleteEmployee called" + objEmployee.toString();
 
    }
}

6- Employee

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
package com.siddhu.model;
 
public class Employee {
    public String id;
    public String name;
 
 
    public Employee() {
    }
 
    public Employee(String id) {
        this.id = id;  
        this.name= "default";
    }
     
    public Employee(String id, String name) {
        this.id = id;
        this.name= name;
    }
 
 
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name  + "]";
    }
 
 
    public String getId() {
        return id;
    }
 
 
    public void setId(String id) {
        this.id = id;
    }
 
 
    public String getName() {
        return name;
    }
 
 
    public void setName(String name) {
        this.name = name;
    }
 
 
 
 
}

Make sure to compile the project usign maven clean install and run the application

Once the application is running make sure that we get response from our getEmployee url

Now use below url to get the swagger-ui.html

http://localhost:8080/swagger-ui.html

On clicking on below url we will get our project OpenAPI 3 specific yaml or JSON files.

http://localhost:8080/v2/api-docs

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
{
"swagger": "2.0",
"info": {
"description": "This is siddhu description swagger example",
"version": "1.1",
"title": "This is title for siddhu swagger example"
},
"host": "localhost:8080",
"basePath": "/",
"tags": [
{
"name": "basic-error-controller",
"description": "Basic Error Controller"
},
{
"name": "index-controller",
"description": "Index Controller"
}
],
"paths": {
"/deleteEmployee/{id}": {
"delete": {
"tags": [
"index-controller"
],
"summary": "To post the employee",
"operationId": "deleteEmployeeUsingDELETE",
"produces": [
"*/*"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "id",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Employee"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"deprecated": false
}
},
"/error": {
"get": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingGET",
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
},
"head": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingHEAD",
"consumes": [
"application/json"
],
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"deprecated": false
},
"post": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingPOST",
"consumes": [
"application/json"
],
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
},
"put": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingPUT",
"consumes": [
"application/json"
],
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
},
"delete": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingDELETE",
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"deprecated": false
},
"options": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingOPTIONS",
"consumes": [
"application/json"
],
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"deprecated": false
},
"patch": {
"tags": [
"basic-error-controller"
],
"summary": "errorHtml",
"operationId": "errorHtmlUsingPATCH",
"consumes": [
"application/json"
],
"produces": [
"text/html"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ModelAndView"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"deprecated": false
}
},
"/getEmployee": {
"get": {
"tags": [
"index-controller"
],
"summary": "To get the employee",
"operationId": "getEmployeeUsingGET",
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Employee"
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
},
"/insertEmployee/{id}/{name}": {
"post": {
"tags": [
"index-controller"
],
"summary": "To post the employee",
"operationId": "insertEmployeeUsingPOST",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "id",
"required": true,
"type": "string"
},
{
"name": "name",
"in": "path",
"description": "name",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Employee"
}
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
},
"/updateEmployee/{id}/{name}": {
"put": {
"tags": [
"index-controller"
],
"summary": "To post the employee",
"operationId": "updateEmployeeUsingPUT",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "id",
"required": true,
"type": "string"
},
{
"name": "name",
"in": "path",
"description": "name",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Employee"
}
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
}
},
"definitions": {
"Employee": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "string",
"description": "1"
},
"name": {
"type": "string",
"description": "siddhu"
}
},
"title": "Employee",
"description": "This is employee model"
},
"ModelAndView": {
"type": "object",
"properties": {
"empty": {
"type": "boolean"
},
"model": {
"type": "object"
},
"modelMap": {
"type": "object",
"additionalProperties": {
"type": "object"
}
},
"reference": {
"type": "boolean"
},
"status": {
"type": "string",
"enum": [
"100 CONTINUE",
"101 SWITCHING_PROTOCOLS",
"102 PROCESSING",
"103 CHECKPOINT",
"200 OK",
"201 CREATED",
"202 ACCEPTED",
"203 NON_AUTHORITATIVE_INFORMATION",
"204 NO_CONTENT",
"205 RESET_CONTENT",
"206 PARTIAL_CONTENT",
"207 MULTI_STATUS",
"208 ALREADY_REPORTED",
"226 IM_USED",
"300 MULTIPLE_CHOICES",
"301 MOVED_PERMANENTLY",
"302 FOUND",
"302 MOVED_TEMPORARILY",
"303 SEE_OTHER",
"304 NOT_MODIFIED",
"305 USE_PROXY",
"307 TEMPORARY_REDIRECT",
"308 PERMANENT_REDIRECT",
"400 BAD_REQUEST",
"401 UNAUTHORIZED",
"402 PAYMENT_REQUIRED",
"403 FORBIDDEN",
"404 NOT_FOUND",
"405 METHOD_NOT_ALLOWED",
"406 NOT_ACCEPTABLE",
"407 PROXY_AUTHENTICATION_REQUIRED",
"408 REQUEST_TIMEOUT",
"409 CONFLICT",
"410 GONE",
"411 LENGTH_REQUIRED",
"412 PRECONDITION_FAILED",
"413 PAYLOAD_TOO_LARGE",
"413 REQUEST_ENTITY_TOO_LARGE",
"414 URI_TOO_LONG",
"414 REQUEST_URI_TOO_LONG",
"415 UNSUPPORTED_MEDIA_TYPE",
"416 REQUESTED_RANGE_NOT_SATISFIABLE",
"417 EXPECTATION_FAILED",
"418 I_AM_A_TEAPOT",
"419 INSUFFICIENT_SPACE_ON_RESOURCE",
"420 METHOD_FAILURE",
"421 DESTINATION_LOCKED",
"422 UNPROCESSABLE_ENTITY",
"423 LOCKED",
"424 FAILED_DEPENDENCY",
"425 TOO_EARLY",
"426 UPGRADE_REQUIRED",
"428 PRECONDITION_REQUIRED",
"429 TOO_MANY_REQUESTS",
"431 REQUEST_HEADER_FIELDS_TOO_LARGE",
"451 UNAVAILABLE_FOR_LEGAL_REASONS",
"500 INTERNAL_SERVER_ERROR",
"501 NOT_IMPLEMENTED",
"502 BAD_GATEWAY",
"503 SERVICE_UNAVAILABLE",
"504 GATEWAY_TIMEOUT",
"505 HTTP_VERSION_NOT_SUPPORTED",
"506 VARIANT_ALSO_NEGOTIATES",
"507 INSUFFICIENT_STORAGE",
"508 LOOP_DETECTED",
"509 BANDWIDTH_LIMIT_EXCEEDED",
"510 NOT_EXTENDED",
"511 NETWORK_AUTHENTICATION_REQUIRED"
]
},
"view": {
"$ref": "#/definitions/View"
},
"viewName": {
"type": "string"
}
},
"title": "ModelAndView"
},
"View": {
"type": "object",
"properties": {
"contentType": {
"type": "string"
}
},
"title": "View"
}
}
}

OR

Employee.yaml

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
---
swagger: '2.0'
info:
  description: This is siddhu description swagger example
  version: '1.1'
  title: This is title for siddhu swagger example
host: localhost:8080
basePath: "/"
tags:
- name: basic-error-controller
  description: Basic Error Controller
- name: index-controller
  description: Index Controller
paths:
  "/deleteEmployee/{id}":
    delete:
      tags:
      - index-controller
      summary: To post the employee
      operationId: deleteEmployeeUsingDELETE
      produces:
      - "*/*"
      parameters:
      - name: id
        in: path
        description: id
        required: true
        type: string
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/Employee"
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      deprecated: false
  "/error":
    get:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingGET
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
    head:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingHEAD
      consumes:
      - application/json
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      deprecated: false
    post:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPOST
      consumes:
      - application/json
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
    put:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPUT
      consumes:
      - application/json
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
    delete:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingDELETE
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      deprecated: false
    options:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingOPTIONS
      consumes:
      - application/json
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      deprecated: false
    patch:
      tags:
      - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPATCH
      consumes:
      - application/json
      produces:
      - text/html
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/ModelAndView"
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      deprecated: false
  "/getEmployee":
    get:
      tags:
      - index-controller
      summary: To get the employee
      operationId: getEmployeeUsingGET
      produces:
      - "*/*"
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/Employee"
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
  "/insertEmployee/{id}/{name}":
    post:
      tags:
      - index-controller
      summary: To post the employee
      operationId: insertEmployeeUsingPOST
      consumes:
      - application/json
      produces:
      - "*/*"
      parameters:
      - name: id
        in: path
        description: id
        required: true
        type: string
      - name: name
        in: path
        description: name
        required: true
        type: string
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/Employee"
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
  "/updateEmployee/{id}/{name}":
    put:
      tags:
      - index-controller
      summary: To post the employee
      operationId: updateEmployeeUsingPUT
      consumes:
      - application/json
      produces:
      - "*/*"
      parameters:
      - name: id
        in: path
        description: id
        required: true
        type: string
      - name: name
        in: path
        description: name
        required: true
        type: string
      responses:
        '200':
          description: OK
          schema:
            "$ref": "#/definitions/Employee"
        '201':
          description: Created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      deprecated: false
definitions:
  Employee:
    type: object
    required:
    - id
    - name
    properties:
      id:
        type: string
        description: '1'
      name:
        type: string
        description: siddhu
    title: Employee
    description: This is employee model
  ModelAndView:
    type: object
    properties:
      empty:
        type: boolean
      model:
        type: object
      modelMap:
        type: object
        additionalProperties:
          type: object
      reference:
        type: boolean
      status:
        type: string
        enum:
        - 100 CONTINUE
        - 101 SWITCHING_PROTOCOLS
        - 102 PROCESSING
        - 103 CHECKPOINT
        - 200 OK
        - 201 CREATED
        - 202 ACCEPTED
        - 203 NON_AUTHORITATIVE_INFORMATION
        - 204 NO_CONTENT
        - 205 RESET_CONTENT
        - 206 PARTIAL_CONTENT
        - 207 MULTI_STATUS
        - 208 ALREADY_REPORTED
        - 226 IM_USED
        - 300 MULTIPLE_CHOICES
        - 301 MOVED_PERMANENTLY
        - 302 FOUND
        - 302 MOVED_TEMPORARILY
        - 303 SEE_OTHER
        - 304 NOT_MODIFIED
        - 305 USE_PROXY
        - 307 TEMPORARY_REDIRECT
        - 308 PERMANENT_REDIRECT
        - 400 BAD_REQUEST
        - 401 UNAUTHORIZED
        - 402 PAYMENT_REQUIRED
        - 403 FORBIDDEN
        - 404 NOT_FOUND
        - 405 METHOD_NOT_ALLOWED
        - 406 NOT_ACCEPTABLE
        - 407 PROXY_AUTHENTICATION_REQUIRED
        - 408 REQUEST_TIMEOUT
        - 409 CONFLICT
        - 410 GONE
        - 411 LENGTH_REQUIRED
        - 412 PRECONDITION_FAILED
        - 413 PAYLOAD_TOO_LARGE
        - 413 REQUEST_ENTITY_TOO_LARGE
        - 414 URI_TOO_LONG
        - 414 REQUEST_URI_TOO_LONG
        - 415 UNSUPPORTED_MEDIA_TYPE
        - 416 REQUESTED_RANGE_NOT_SATISFIABLE
        - 417 EXPECTATION_FAILED
        - 418 I_AM_A_TEAPOT
        - 419 INSUFFICIENT_SPACE_ON_RESOURCE
        - 420 METHOD_FAILURE
        - 421 DESTINATION_LOCKED
        - 422 UNPROCESSABLE_ENTITY
        - 423 LOCKED
        - 424 FAILED_DEPENDENCY
        - 425 TOO_EARLY
        - 426 UPGRADE_REQUIRED
        - 428 PRECONDITION_REQUIRED
        - 429 TOO_MANY_REQUESTS
        - 431 REQUEST_HEADER_FIELDS_TOO_LARGE
        - 451 UNAVAILABLE_FOR_LEGAL_REASONS
        - 500 INTERNAL_SERVER_ERROR
        - 501 NOT_IMPLEMENTED
        - 502 BAD_GATEWAY
        - 503 SERVICE_UNAVAILABLE
        - 504 GATEWAY_TIMEOUT
        - 505 HTTP_VERSION_NOT_SUPPORTED
        - 506 VARIANT_ALSO_NEGOTIATES
        - 507 INSUFFICIENT_STORAGE
        - 508 LOOP_DETECTED
        - 509 BANDWIDTH_LIMIT_EXCEEDED
        - 510 NOT_EXTENDED
        - 511 NETWORK_AUTHENTICATION_REQUIRED
      view:
        "$ref": "#/definitions/View"
      viewName:
        type: string
    title: ModelAndView
  View:
    type: object
    properties:
      contentType:
        type: string
    title: View

If you want you can also get the yaml file. I used online converter to convert it to yaml

https://www.json2yaml.com/

Now lets see our swagger-ui.html url

Hit on all the options and check click on Try it out

Now lets chanage few of the code for more beautification of our documentation

Note: You can get this code from below url
https://github.com/shdhumale/simple-springboot-swagger.git


Generally we use to have the swagger running only for our Dev and Testing environment and not for our Production.

This can be easily achieved by just adding one annotation in our config files as shown below

@Profile(“!production”)

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
package com.siddhu.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
 
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
@Profile("!production")
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .build();
              
    }
    private ApiInfo apiInfo()
    {
        return new ApiInfoBuilder().title("This is title for siddhu swagger example").description("This is siddhu description swagger example").version("1.1").build();
    }
     
}

And try to run our springboot project by modifying runconfiguration as shown below.

If we run the application in production mode we will get this screen when we try to access the url

http://localhost:8080/swagger-ui.html

No comments: