Extending WebMvcConfigurerAdapter to register Custom AnnotationFormatterFactory

@EnableWebMvc
@Configuration
@Import(MyViewConfig.class)
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters (FormatterRegistry registry) {
AddressFormatAnnotationFormatterFactory factory = new
AddressFormatAnnotationFormatterFactory();
registry.addFormatterForFieldAnnotation(factory);
}
@Bean
public CustomerController customerController () {
return new CustomerController();
}
@Bean
public CustomerDataService userService () {
return new InMemoryCustomerDataService();
}
}
Original PostExtending WebMvcConfigurerAdapter to add Custom Converter

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
@Import(MyViewConfig.class)
public class TradeConfig extends WebMvcConfigurerAdapter {
@Bean
public TradeController tradeParamRequestController() {
return new TradeController();
}
@Bean
public TradeService tradeService(){
return new TradeService();
}
@Override
public void addFormatters (FormatterRegistry registry) {
registry.addConverter(new TradeIdToTradeConverter(tradeService()));
}
}
Original PostExtending WebMvcConfigurerAdapter to add custom Formatter

@EnableWebMvc
@Configuration
@Import(MyViewConfig.class)
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters (FormatterRegistry registry) {
AddressFormatter addressFormatter = new AddressFormatter();
addressFormatter.setStyle(AddressFormatter.Style.REGION);
registry.addFormatter(addressFormatter);
}
@Bean
public CustomerController customerController () {
return new CustomerController();
}
@Bean
public CustomerDataService userService () {
return new InMemoryCustomerDataService();
}
}
Original PostRegistering a custom Interceptor

@EnableWebMvc
@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors (InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor());
}
@Bean
public AppController tradeController () {
return new AppController();
}
@Bean
public UserService userService () {
return new InMemoryUserService();
}
@Bean
public ViewResolver viewResolver () {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Original PostUsing a custom TaskExecutor for async processing.

package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class AsyncConfigExample {
@Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
@Override
public void configureAsyncSupport (AsyncSupportConfigurer configurer) {
ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
t.setCorePoolSize(10);
t.setMaxPoolSize(100);
t.setQueueCapacity(50);
t.setAllowCoreThreadTimeOut(true);
t.setKeepAliveSeconds(120);
t.initialize();
configurer.setTaskExecutor(t);
}
};
}
public static void main (String[] args) {
SpringApplication.run(AsyncConfigExample.class, args);
}
}
Original PostAdding custom location for static pages in a boot application.

package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableAutoConfiguration
public class AddCustomLocations {
@Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").
addResourceLocations("classpath:/my-custom-location/");
}
};
}
public static void main (String[] args) {
SpringApplication app =
new SpringApplication(AddCustomLocations.class);
app.run(args);
}
}
Original PostWebMvcConfigurerAdapter#addResourceHandlers adds a resource handler for serving static resources based on the specified URL path patterns. ResourceHandlerRegistry stores the registration of resources.

@Bean
public WebMvcConfigurerAdapter webConfigurer () {
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/");
}
};
}
Original PostRegistering a custom HttpMessageConverter

import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
@EnableWebMvc
@ComponentScan("com.logicbig.example")
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters (List<HttpMessageConverter<?>> converters) {
converters.add(new YamlHttpMessageConverter<>());
}
}
Original Post