All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.paradoxical.dropwizard.swagger.SwaggerUIConfigurator Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
package io.paradoxical.dropwizard.swagger;

import com.google.common.collect.ImmutableList;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewMessageBodyWriter;
import io.dropwizard.views.mustache.MustacheViewRenderer;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import lombok.NonNull;

import java.util.function.Function;

public class SwaggerUIConfigurator {
    private final SwaggerResourcesLocator.Factory swaggerResourcesLocatorFactory;

    @SuppressWarnings("WeakerAccess")
    public SwaggerUIConfigurator(@NonNull final SwaggerResourcesLocator.Factory swaggerResourcesLocatorFactory) {
        this.swaggerResourcesLocatorFactory = swaggerResourcesLocatorFactory;
    }

    @SuppressWarnings("WeakerAccess")
    public SwaggerUIConfigurator(@NonNull final SwaggerResourcesLocator swaggerResourcesLocator) {
        this(env -> swaggerResourcesLocator);
    }

    @SuppressWarnings("unused")
    public static SwaggerUIConfigurator forConfig(@NonNull final SwaggerConfiguration swaggerConfiguration) {
        return new SwaggerUIConfigurator(new DefaultSwaggerResourcesLocator(swaggerConfiguration));
    }

    @SuppressWarnings("unused")
    public static SwaggerUIConfigurator forConfig(@NonNull final Function envConfigFunction) {
        return new SwaggerUIConfigurator(env -> new DefaultSwaggerResourcesLocator(envConfigFunction.apply(env)));
    }

    public void configure(Environment environment, JerseyEnvironment jerseyEnvironment) {

        final ViewMessageBodyWriter viewMessageBodyWriter =
            new ViewMessageBodyWriter(environment.metrics(),
                                      ImmutableList.of(new MustacheViewRenderer()));

        jerseyEnvironment.register(viewMessageBodyWriter);
        jerseyEnvironment.register(new SwaggerSerializers());
        jerseyEnvironment.register(swaggerResourcesLocatorFactory.forEnvironment(environment));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy