Skip to content

Instantly share code, notes, and snippets.

@mlaccetti
Created August 13, 2013 14:40
Show Gist options
  • Save mlaccetti/6221808 to your computer and use it in GitHub Desktop.
Save mlaccetti/6221808 to your computer and use it in GitHub Desktop.
Getting swagger to run using SpringMVC and 100% annotations.
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;
}
}
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment