support for swagger 2 boot 2.x
This commit is contained in:
parent
42fac5dbcf
commit
de3a7e4727
|
@ -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)
|
|
@ -15,6 +15,7 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@ -23,6 +24,7 @@
|
|||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--JSF -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
|
@ -30,17 +32,13 @@
|
|||
<version>2.3.7</version>
|
||||
</dependency>
|
||||
|
||||
<!--Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- ROME for RSS -->
|
||||
<dependency>
|
||||
<groupId>com.rometools</groupId>
|
||||
|
@ -48,6 +46,7 @@
|
|||
<version>${rome.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Validation -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
|
@ -56,6 +55,23 @@
|
|||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Fox 2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -72,6 +88,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.fox.version>2.9.2</spring.fox.version>
|
||||
<!-- ROME for RSS -->
|
||||
<rome.version>1.10.0</rome.version>
|
||||
<start-class>com.baeldung.springbootmvc.SpringBootMvcApplication</start-class>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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";
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue