SpringCloud manages Swagger uniformly

This is the third day of my participation in the August More Text Challenge

Swagger can be configured on the Gateway for all service services without swagger- UI.

From there it is gateway configuration, and the rest of eurekaclient can be configured as normal standalone projects

Start by introducing the Swagger JAR package into the Gateway microservice you already created

        <dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.9.2</version>
		</dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
Copy the code

Declare that you do not need to configure the following in the Gateway, or you will get crazy errors

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(a){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.basecloud.auth.controller"))
                .build();
    }
    private ApiInfo apiInfo(a) {
        return new ApiInfoBuilder()
                // Page title
                .title("Auth Restful Api")
                / / founder
                .contact(new Contact("yujian"."http://www.basecloud.com/".""))
                / / version number
                .version("1.0")
                / / description
                .description("API description") .build(); }} By default, swagger requests some interfaces@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
    @Autowired
    private SwaggerResourcesConfig swaggerResources;

    @Autowired(required = false)
    private SecurityConfiguration securityConfiguration;
    @Autowired(required = false)
    private UiConfiguration uiConfiguration;

    @GetMapping("/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("")
    public Mono<ResponseEntity> swaggerResources(a) {
        return Mono.just((newResponseEntity<>(swaggerResources.get(), HttpStatus.OK))); }}Copy the code

Take the configuration file as an example and forward it according to the path name, so that the gateway forwards tohttp://localhost:8099/auth/v2/api-docs path, will request to register for BASECLOUD – AUTH services (localhost: 8091 / v2 / API docs) to get the json Gateway forwarding address:Actual Swagger address (Auth service)Ok see here probably can understand how to unified management swagger, I also configure a separate forwarding path

swagger:
   names: auth,shop
Copy the code

Then configure the collection of swaggerResource, with the configuration classes below

@Component
@Primary
public class SwaggerResourcesConfig implements SwaggerResourcesProvider{

    @Value("${swagger.names}")
    private String[] apiNames;
    @Override
    public List<SwaggerResource> get(a) {
        List<SwaggerResource> collect = Arrays.asList(apiNames).stream().map(name -> {
            SwaggerResource swaggerResource = swaggerResource(name,  "/"+name+"/v2/api-docs");
            return swaggerResource;
        }).collect(Collectors.toList());

        return collect;
    }

    private SwaggerResource swaggerResource(String serviceId, String location){

        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(serviceId);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion("2.0");
        returnswaggerResource; }}Copy the code