Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created July 17, 2013 16:13
Show Gist options
  • Select an option

  • Save dwelch2344/6022046 to your computer and use it in GitHub Desktop.

Select an option

Save dwelch2344/6022046 to your computer and use it in GitHub Desktop.
Originally I just added a @configuration file for SprintData-Rest and got my entities working in my existing SpringMVC app, but then I realized it was stomping on the web portion of my app (mapped to / ) So I've reworked the initializer to add a separate DispatcherServlet. I added the code around line 39 to try and add SpringData-Rest with a pre…
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration;
// @Configuration
public class RestConfig extends RepositoryRestMvcConfiguration {
public RestConfig() {
System.out.println("Rest Config");
}
}
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.rest.webmvc.RepositoryRestExporterServlet;
import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;
/**
* Bootstraps the Spring Application via Spring's automagic {@link SpringServletContainerInitializer}.
* See {@code WebApplicationInitializer} for more information.
*
*/
@Slf4j
public class SpringWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = getWebAppContext();
addSecurityFilter(servletContext);
addSocialFilter(servletContext);
addDispatcherServlet(servletContext, ctx);
servletContext.addListener(new ContextLoaderListener(ctx));
log.info("Successfully Configured Web Application");
// FIXME get this working?
AnnotationConfigWebApplicationContext restCtx = new AnnotationConfigWebApplicationContext();
restCtx.setConfigLocation(RestConfig.class.getName());
RepositoryRestExporterServlet exporter = new RepositoryRestExporterServlet(restCtx);
exporter.setContextConfigLocation(RestConfig.class.getName());
ServletRegistration.Dynamic reg = servletContext.addServlet("rest-exporter", exporter);
reg.setLoadOnStartup(1);
reg.addMapping("/api/*");
}
private AnnotationConfigWebApplicationContext getWebAppContext(){
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan( getClass().getPackage().getName() );
return root;
}
private void addDispatcherServlet(ServletContext servletContext, WebApplicationContext root) {
ServletRegistration.Dynamic appServlet = servletContext.addServlet("dispatchServlet", new DispatcherServlet(root));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
log.warn("Mapping conflict encountered when mapping DispatcherServlet to '/' ");
throw new IllegalStateException("Failed mapping dispatcher to root");
}
}
private void addSecurityFilter(ServletContext servletContext){
// Add Spring Security Filter
DelegatingFilterProxy securityFilter = new DelegatingFilterProxy();
servletContext.addFilter("springSecurityFilterChain", securityFilter)
.addMappingForUrlPatterns(null,false,"/*");
log.info("Wired in springSecurityFilterChain delegate");
}
/**
* Note: this <b>must</b> be called AFTER the security filter has been added
*/
private void addSocialFilter(ServletContext servletContext) {
DelegatingFilterProxy socialFilter = new DelegatingFilterProxy();
servletContext.addFilter("socialClientAuthenticationFilter", socialFilter)
.addMappingForUrlPatterns(null, false, SocialConfig.CALLBACK_URL);
log.info("Wired in socailFilter delegate to " + SocialConfig.CALLBACK_URL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment