support for swagger 2 boot 2.x

This commit is contained in:
thoughtscript
2018-11-14 20:39:48 -08:00
parent 42fac5dbcf
commit de3a7e4727
8 changed files with 200 additions and 5 deletions

View File

@@ -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);
}
}

View File

@@ -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";
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}