From de3a7e4727aeb0b454d5ff7947fe19cb645017af Mon Sep 17 00:00:00 2001 From: thoughtscript Date: Wed, 14 Nov 2018 20:39:48 -0800 Subject: [PATCH] support for swagger 2 boot 2.x --- spring-boot-mvc/README.md | 1 + spring-boot-mvc/pom.xml | 27 +++++-- .../com/baeldung/swaggerboot/Application.java | 15 ++++ .../com/baeldung/swaggerboot/Constants.java | 16 ++++ .../configuration/SpringFoxConfig.java | 73 +++++++++++++++++++ .../controller/RegularRestController.java | 22 ++++++ .../services/RegularWebService.java | 20 +++++ .../swaggerboot/transfer/CustomResponse.java | 31 ++++++++ 8 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Application.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Constants.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/configuration/SpringFoxConfig.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/controller/RegularRestController.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/services/RegularWebService.java create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/transfer/CustomResponse.java diff --git a/spring-boot-mvc/README.md b/spring-boot-mvc/README.md index e7b42f8f50..bf32e4fc7c 100644 --- a/spring-boot-mvc/README.md +++ b/spring-boot-mvc/README.md @@ -9,3 +9,4 @@ - [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed) - [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao) - [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache) +- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) \ No newline at end of file diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index 5f6cdff85b..b219e53431 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -15,6 +15,7 @@ + org.springframework.boot spring-boot-starter-web @@ -23,6 +24,7 @@ org.apache.tomcat.embed tomcat-embed-jasper + org.glassfish @@ -30,17 +32,13 @@ 2.3.7 + org.springframework.boot spring-boot-starter-test test - - org.springframework.boot - spring-boot-starter-validation - - com.rometools @@ -48,6 +46,7 @@ ${rome.version} + org.hibernate.validator hibernate-validator @@ -56,6 +55,23 @@ javax.validation validation-api + + org.springframework.boot + spring-boot-starter-validation + + + + + io.springfox + springfox-swagger2 + ${spring.fox.version} + + + io.springfox + springfox-swagger-ui + ${spring.fox.version} + + @@ -72,6 +88,7 @@ + 2.9.2 1.10.0 com.baeldung.springbootmvc.SpringBootMvcApplication diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Application.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Application.java new file mode 100644 index 0000000000..2161597c4e --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Application.java @@ -0,0 +1,15 @@ +package com.baeldung.swaggerboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages = {"com.baeldung"}) +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Constants.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Constants.java new file mode 100644 index 0000000000..14b4162713 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/Constants.java @@ -0,0 +1,16 @@ +package com.baeldung.swaggerboot; + +public class Constants { + + public static final String DEFAULT_GREETING = "Howdy Cosmic Spheroid!"; + public static final String DEFAULT_ERROR = "Fail!"; + + /** + * API Endpoint. + */ + + public static final String REACTIVE_REST_URL = "/reactiverest"; + public static final String FUNCTIONAL_URL = "/functional"; + public static final String REGULAR_REST_URL = "/regularrest"; + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/configuration/SpringFoxConfig.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/configuration/SpringFoxConfig.java new file mode 100644 index 0000000000..babe70580c --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/configuration/SpringFoxConfig.java @@ -0,0 +1,73 @@ +package com.baeldung.swaggerboot.configuration; + +import com.fasterxml.classmate.TypeResolver; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger.web.*; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +import java.util.Collections; + +@Configuration +@EnableSwagger2 +@ComponentScan("com.baeldung.swaggerboot.controller") +public class SpringFoxConfig { + + @Autowired + private TypeResolver typeResolver; + + private ApiInfo apiInfo() { + return new ApiInfo( + "My REST API", + "Some custom description of API.", + "API TOS", + "Terms of service", + new Contact("John Doe", "www.example.com", "myeaddress@company.com"), + "License of API", + "API license URL", + Collections.emptyList()); + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + + /** + * SwaggerUI information + */ + + @Bean + UiConfiguration uiConfig() { + return UiConfigurationBuilder.builder() + .deepLinking(true) + .displayOperationId(false) + .defaultModelsExpandDepth(1) + .defaultModelExpandDepth(1) + .defaultModelRendering(ModelRendering.EXAMPLE) + .displayRequestDuration(false) + .docExpansion(DocExpansion.NONE) + .filter(false) + .maxDisplayedTags(null) + .operationsSorter(OperationsSorter.ALPHA) + .showExtensions(false) + .tagsSorter(TagsSorter.ALPHA) + .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS) + .validatorUrl(null) + .build(); + } + +} diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/controller/RegularRestController.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/controller/RegularRestController.java new file mode 100644 index 0000000000..537e16d146 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/controller/RegularRestController.java @@ -0,0 +1,22 @@ +package com.baeldung.swaggerboot.controller; + +import com.baeldung.swaggerboot.services.RegularWebService; +import com.baeldung.swaggerboot.transfer.CustomResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import static com.baeldung.swaggerboot.Constants.REGULAR_REST_URL; + +@RestController +public class RegularRestController { + + @Autowired + RegularWebService regularWebService; + + @GetMapping(REGULAR_REST_URL) + public CustomResponse getSession() { + return regularWebService.example(); + } + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/services/RegularWebService.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/services/RegularWebService.java new file mode 100644 index 0000000000..d13ed7a6a9 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/services/RegularWebService.java @@ -0,0 +1,20 @@ +package com.baeldung.swaggerboot.services; + +import com.baeldung.swaggerboot.transfer.CustomResponse; +import org.springframework.stereotype.Service; + +import static com.baeldung.swaggerboot.Constants.DEFAULT_ERROR; +import static com.baeldung.swaggerboot.Constants.DEFAULT_GREETING; + +@Service +public class RegularWebService { + + public CustomResponse example() { + try { + return new CustomResponse(0, DEFAULT_GREETING); + } catch (Exception ex) { + return new CustomResponse(0, DEFAULT_ERROR); + } + } + +} \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/transfer/CustomResponse.java b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/transfer/CustomResponse.java new file mode 100644 index 0000000000..d09e9f935e --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/swaggerboot/transfer/CustomResponse.java @@ -0,0 +1,31 @@ +package com.baeldung.swaggerboot.transfer; + +public class CustomResponse { + + private int id; + private String note; + + public CustomResponse() {} + + public CustomResponse(int id, String note) { + this.id = id; + this.note = note; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNote() { + return note; + } + + public void setNote(String note) { + this.note = note; + } + +}