Skip to content

Commit b970aaa

Browse files
authored
Merge pull request eugenp#5687 from Thoughtscript/BAEL-1781
BAEL-1781: Support for Swagger 2 using Spring Boot 2.x
2 parents 6e94e2b + de3a7e4 commit b970aaa

8 files changed

Lines changed: 200 additions & 5 deletions

File tree

spring-boot-mvc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed)
1010
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
1111
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
12+
- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)

spring-boot-mvc/pom.xml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</parent>
1616

1717
<dependencies>
18+
1819
<dependency>
1920
<groupId>org.springframework.boot</groupId>
2021
<artifactId>spring-boot-starter-web</artifactId>
@@ -23,31 +24,29 @@
2324
<groupId>org.apache.tomcat.embed</groupId>
2425
<artifactId>tomcat-embed-jasper</artifactId>
2526
</dependency>
27+
2628
<!--JSF -->
2729
<dependency>
2830
<groupId>org.glassfish</groupId>
2931
<artifactId>javax.faces</artifactId>
3032
<version>2.3.7</version>
3133
</dependency>
3234

35+
<!--Test -->
3336
<dependency>
3437
<groupId>org.springframework.boot</groupId>
3538
<artifactId>spring-boot-starter-test</artifactId>
3639
<scope>test</scope>
3740
</dependency>
3841

39-
<dependency>
40-
<groupId>org.springframework.boot</groupId>
41-
<artifactId>spring-boot-starter-validation</artifactId>
42-
</dependency>
43-
4442
<!-- ROME for RSS -->
4543
<dependency>
4644
<groupId>com.rometools</groupId>
4745
<artifactId>rome</artifactId>
4846
<version>${rome.version}</version>
4947
</dependency>
5048

49+
<!--Validation -->
5150
<dependency>
5251
<groupId>org.hibernate.validator</groupId>
5352
<artifactId>hibernate-validator</artifactId>
@@ -56,6 +55,23 @@
5655
<groupId>javax.validation</groupId>
5756
<artifactId>validation-api</artifactId>
5857
</dependency>
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-validation</artifactId>
61+
</dependency>
62+
63+
<!-- Spring Fox 2 -->
64+
<dependency>
65+
<groupId>io.springfox</groupId>
66+
<artifactId>springfox-swagger2</artifactId>
67+
<version>${spring.fox.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>io.springfox</groupId>
71+
<artifactId>springfox-swagger-ui</artifactId>
72+
<version>${spring.fox.version}</version>
73+
</dependency>
74+
5975
</dependencies>
6076

6177
<build>
@@ -72,6 +88,7 @@
7288
</build>
7389

7490
<properties>
91+
<spring.fox.version>2.9.2</spring.fox.version>
7592
<!-- ROME for RSS -->
7693
<rome.version>1.10.0</rome.version>
7794
<start-class>com.baeldung.springbootmvc.SpringBootMvcApplication</start-class>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.swaggerboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
@SpringBootApplication
8+
@ComponentScan(basePackages = {"com.baeldung"})
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.swaggerboot;
2+
3+
public class Constants {
4+
5+
public static final String DEFAULT_GREETING = "Howdy Cosmic Spheroid!";
6+
public static final String DEFAULT_ERROR = "Fail!";
7+
8+
/**
9+
* API Endpoint.
10+
*/
11+
12+
public static final String REACTIVE_REST_URL = "/reactiverest";
13+
public static final String FUNCTIONAL_URL = "/functional";
14+
public static final String REGULAR_REST_URL = "/regularrest";
15+
16+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.swaggerboot.configuration;
2+
3+
import com.fasterxml.classmate.TypeResolver;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import springfox.documentation.builders.PathSelectors;
9+
import springfox.documentation.builders.RequestHandlerSelectors;
10+
import springfox.documentation.service.ApiInfo;
11+
import springfox.documentation.service.Contact;
12+
import springfox.documentation.spi.DocumentationType;
13+
import springfox.documentation.spring.web.plugins.Docket;
14+
import springfox.documentation.swagger.web.*;
15+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
16+
17+
import java.util.Collections;
18+
19+
@Configuration
20+
@EnableSwagger2
21+
@ComponentScan("com.baeldung.swaggerboot.controller")
22+
public class SpringFoxConfig {
23+
24+
@Autowired
25+
private TypeResolver typeResolver;
26+
27+
private ApiInfo apiInfo() {
28+
return new ApiInfo(
29+
"My REST API",
30+
"Some custom description of API.",
31+
"API TOS",
32+
"Terms of service",
33+
new Contact("John Doe", "www.example.com", "[email protected]"),
34+
"License of API",
35+
"API license URL",
36+
Collections.emptyList());
37+
}
38+
39+
@Bean
40+
public Docket api() {
41+
return new Docket(DocumentationType.SWAGGER_2)
42+
.apiInfo(apiInfo())
43+
.select()
44+
.apis(RequestHandlerSelectors.any())
45+
.paths(PathSelectors.any())
46+
.build();
47+
}
48+
49+
/**
50+
* SwaggerUI information
51+
*/
52+
53+
@Bean
54+
UiConfiguration uiConfig() {
55+
return UiConfigurationBuilder.builder()
56+
.deepLinking(true)
57+
.displayOperationId(false)
58+
.defaultModelsExpandDepth(1)
59+
.defaultModelExpandDepth(1)
60+
.defaultModelRendering(ModelRendering.EXAMPLE)
61+
.displayRequestDuration(false)
62+
.docExpansion(DocExpansion.NONE)
63+
.filter(false)
64+
.maxDisplayedTags(null)
65+
.operationsSorter(OperationsSorter.ALPHA)
66+
.showExtensions(false)
67+
.tagsSorter(TagsSorter.ALPHA)
68+
.supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
69+
.validatorUrl(null)
70+
.build();
71+
}
72+
73+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.swaggerboot.controller;
2+
3+
import com.baeldung.swaggerboot.services.RegularWebService;
4+
import com.baeldung.swaggerboot.transfer.CustomResponse;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import static com.baeldung.swaggerboot.Constants.REGULAR_REST_URL;
10+
11+
@RestController
12+
public class RegularRestController {
13+
14+
@Autowired
15+
RegularWebService regularWebService;
16+
17+
@GetMapping(REGULAR_REST_URL)
18+
public CustomResponse getSession() {
19+
return regularWebService.example();
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.swaggerboot.services;
2+
3+
import com.baeldung.swaggerboot.transfer.CustomResponse;
4+
import org.springframework.stereotype.Service;
5+
6+
import static com.baeldung.swaggerboot.Constants.DEFAULT_ERROR;
7+
import static com.baeldung.swaggerboot.Constants.DEFAULT_GREETING;
8+
9+
@Service
10+
public class RegularWebService {
11+
12+
public CustomResponse example() {
13+
try {
14+
return new CustomResponse(0, DEFAULT_GREETING);
15+
} catch (Exception ex) {
16+
return new CustomResponse(0, DEFAULT_ERROR);
17+
}
18+
}
19+
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.swaggerboot.transfer;
2+
3+
public class CustomResponse {
4+
5+
private int id;
6+
private String note;
7+
8+
public CustomResponse() {}
9+
10+
public CustomResponse(int id, String note) {
11+
this.id = id;
12+
this.note = note;
13+
}
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getNote() {
24+
return note;
25+
}
26+
27+
public void setNote(String note) {
28+
this.note = note;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)