org.paradise.microservice.SwaggerConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.paradise.microservice.SwaggerConfig.java

Source

package org.paradise.microservice;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.schema.AlternateTypeRules.newRule;

/**
 * Created by terrence on 3/01/2016.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api() {

        ApiInfo info = new ApiInfo("QR as Postal Address API", "API query DPID of Australian Postal Address",
                "1.0.0", "https://opensource.org/ToS", "Terrence Miao <TerrenceMiao@users.noreply.github.com>",
                "Apache License 2.0", "http://www.apache.org/licenses/LICENSE-2.0");

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(info).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build().pathMapping("/")
                .directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
                .alternateTypeRules(newRule(
                        typeResolver.resolve(DeferredResult.class,
                                typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                        typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET,
                        newArrayList(new ResponseMessageBuilder().code(HttpStatus.INTERNAL_SERVER_ERROR.value())
                                .message("500 message").responseModel(new ModelRef("Error")).build()))
                .securitySchemes(newArrayList(apiKey())).securityContexts(newArrayList(securityContext()))
                .enableUrlTemplating(true);
        //                .globalOperationParameters(
        //                        newArrayList(new ParameterBuilder()
        //                                .name("someGlobalParameter")
        //                                .description("Description of someGlobalParameter")
        //                                .modelRef(new ModelRef("string"))
        //                                .parameterType("query")
        //                                .required(false)
        //                                .build()));

    }

    private ApiKey apiKey() {

        return new ApiKey("mykey", "api_key", "header");
    }

    private SecurityContext securityContext() {

        return SecurityContext.builder().securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/anyPath.*")).build();
    }

    List<SecurityReference> defaultAuth() {

        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;

        return newArrayList(new SecurityReference("mykey", authorizationScopes));
    }

    @Bean
    SecurityConfiguration security() {

        return new SecurityConfiguration("test-app-client-id", "test-app-client-secret", "test-app-realm",
                "test-app", "apiKey", ApiKeyVehicle.HEADER, ",");
    }

    @Bean
    UiConfiguration uiConfig() {

        return new UiConfiguration("validatorUrl");
    }

}