octavian-nita
8/13/2013 - 2:40 PM

Getting swagger to run using SpringMVC and 100% annotations.

Getting swagger to run using SpringMVC and 100% annotations.

import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import lombok.extern.slf4j.Slf4j;

import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextCleanupListener;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import com.mangofactory.swagger.configuration.DocumentationConfig;

@Slf4j
public class WebAppInitializer implements WebApplicationInitializer {
  private AnnotationConfigWebApplicationContext ctx = null;
  
  @Override
  public void onStartup(ServletContext sc) throws ServletException {
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();

    log.debug("starting up...");
    
    System.setProperty("spring.profiles.active", "web");

    // Create the 'root' Spring application context
    ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(SpringWebConfiguration.class, DocumentationConfig.class);

    // Manages the lifecycle
    sc.addListener(new ContextLoaderListener(ctx));
    sc.addListener(new ContextCleanupListener());

    // Spring WebMVC
    ServletRegistration.Dynamic springWebMvc = sc.addServlet("dispatcher", new DispatcherServlet(ctx));
    springWebMvc.setLoadOnStartup(1);
    springWebMvc.addMapping("/*");
    springWebMvc.setAsyncSupported(true);
    
    // Filters
    sc.addFilter("OpenEntityInViewFilter", OpenEntityManagerInViewFilter.class).addMappingForUrlPatterns(null, true, "/*");

    log.debug("initialized.");
  }
  
  @PreDestroy
  protected final void cleanup() {
    if (ctx != null) {
      ctx.close();
    }
  }
}
import java.io.File;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@Profile("web")
@Slf4j
public class SpringWebConfiguration extends SpringBaseWebConfiguration {
  public SpringWebConfiguration() {
    log.debug("Creating SpringWebConfiguration.");
  }  
  
  @Bean
  public static PropertyPlaceholderConfigurer swaggerProperties() {
    final PropertyPlaceholderConfigurer swaggerProperties = new PropertyPlaceholderConfigurer();
    swaggerProperties.setLocation(new ClassPathResource("swagger.properties"));
    return swaggerProperties;
  }
}