Created
October 4, 2019 09:02
-
-
Save Cvetomird91/b829dfb7347905de14fc52704bb67ab3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebMvc | |
public class WebConfig implements WebMvcConfigurer { | |
//register custom formatters and converters | |
@Override | |
public void addFormatters(FormatterRegistry registry) { | |
// ... | |
} | |
//customize global Validator instance | |
//If you need to have a LocalValidatorFactoryBean injected somewhere, create a bean and mark it with @Primary in order to avoid conflict with the one declared in the MVC configuration. | |
@Override | |
public Validator getValidator() { ... } | |
//register Validator implementations locally | |
@InitBinder | |
protected void initBinder(WebDataBinder binder) { | |
binder.addValidators(new FooValidator()); | |
} | |
// register custom interceptors | |
@Override | |
public void addInterceptors(InterceptorRegistry registry) { | |
registry.addInterceptor(new LocaleChangeInterceptor()); | |
registry.addInterceptor(new ThemeChangeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**"); | |
registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*"); | |
} | |
@Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { | |
configurer.mediaType("json", MediaType.APPLICATION_JSON); | |
configurer.mediaType("xml", MediaType.APPLICATION_XML); | |
} | |
//forward a request to / to a view called home | |
//this is the equivalent of registering a ParameterizableViewController | |
@Override | |
public void addViewControllers(ViewControllerRegistry registry) { | |
registry.addViewController("/").setViewName("home"); | |
} | |
//configure message converters | |
@Override | |
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | |
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() | |
.indentOutput(true) | |
.dateFormat(new SimpleDateFormat("yyyy-MM-dd")) | |
.modulesToInstall(new ParameterNamesModule()); | |
converters.add(new MappingJackson2HttpMessageConverter(builder.build())); | |
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); | |
} | |
//configure the registration of view resolvers | |
//this example configures content negotiation view resolution by using JSP and Jackson as a default View for JSON rendering | |
@Override | |
public void configureViewResolvers(ViewResolverRegistry registry) { | |
registry.enableContentNegotiation(new MappingJackson2JsonView()); | |
registry.freeMarker.cache(false); | |
registry.jsp(); | |
} | |
// in Java-based configuration we can setup the according view template configurer as a bean | |
@Bean | |
public FreeMarkerConfigurer freeMarkerConfigurer() { | |
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); | |
configurer.setTemplateLoaderPath("/freemarker"); | |
return configurer; | |
} | |
//configure path matching | |
@Override | |
public void configurePathMatch(PathMatchConfigurer configurer) { | |
configurer | |
.setUseSuffixPatternMatch(true) | |
.setUseTrailingSlashMatch(false) | |
.setUseRegisteredSuffixPatternMatch(true) | |
.setPathMatcher(antPathMatcher()) | |
.setUrlPathHelper(urlPathHelper()) | |
.addPathPrefix("/api", | |
HandlerTypePredicate.forAnnotation(RestController.class)); | |
} | |
@Bean | |
public UrlPathHelper urlPathHelper() { | |
//... | |
} | |
@Bean | |
public PathMatcher antPathMatcher() { | |
//... | |
} | |
@Override | |
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { | |
configurer.enable(); | |
//or pass a string to set the name of the default servlet name | |
configurer.enable("myCustomDefaultServlet"); | |
} | |
//static resources handling configuration | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler("/resources/**") | |
.addResourceLocations("/public/") | |
.resourceChain(true) | |
.setCachePeriod(86400) | |
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment