com.sds.dmonitor.webapi.config.apidoc.SwaggerConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.sds.dmonitor.webapi.config.apidoc.SwaggerConfiguration.java

Source

package com.sds.dmonitor.webapi.config.apidoc;

import com.google.common.base.Predicates;
import com.sds.dmonitor.webapi.config.Constants;
import com.sds.dmonitor.webapi.config.JHipsterProperties;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.*;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
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.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * Springfox Swagger configuration.
 *
 * Warning! When having a lot of REST endpoints, Springfox can become a performance issue. In that
 * case, you can use a specific Spring profile for this class, so that only front-end developers
 * have access to the Swagger view.
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "jhipster.swagger", name = "enabled")
public class SwaggerConfiguration {

    private final Logger log = LoggerFactory.getLogger(SwaggerConfiguration.class);

    public static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";

    public static final String ADDITIONAL_PATTERN = "/unauth-api/.*";

    /**
     * Swagger Springfox configuration.
     *
     * @param jHipsterProperties the properties of the application
     * @return the Swagger Springfox configuration
     */
    @Bean
    public Docket swaggerSpringfoxDocket(JHipsterProperties jHipsterProperties) {
        log.debug("Starting Swagger");
        StopWatch watch = new StopWatch();
        watch.start();
        Contact contact = new Contact(jHipsterProperties.getSwagger().getContactName(),
                jHipsterProperties.getSwagger().getContactUrl(), jHipsterProperties.getSwagger().getContactEmail());

        ApiInfo apiInfo = new ApiInfo(jHipsterProperties.getSwagger().getTitle(),
                jHipsterProperties.getSwagger().getDescription(), jHipsterProperties.getSwagger().getVersion(),
                jHipsterProperties.getSwagger().getTermsOfServiceUrl(), contact,
                jHipsterProperties.getSwagger().getLicense(), jHipsterProperties.getSwagger().getLicenseUrl());

        Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).forCodeGeneration(true)
                .genericModelSubstitutes(ResponseEntity.class).ignoredParameterTypes(Pageable.class)
                .ignoredParameterTypes(java.sql.Date.class)
                .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
                .directModelSubstitute(java.time.LocalDateTime.class, Date.class).select()
                .paths(Predicates.or(regex(DEFAULT_INCLUDE_PATTERN), regex(ADDITIONAL_PATTERN))).build();
        watch.stop();
        log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
        return docket;
    }
}