SpringBoot___自定义消息转换器、MVC配置

1. 自动配置的消息转换器
 在SptingBoot的源码中的spring-boot-autoconfig的Jar包下,我们可以找到org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.class文件,这个类就是自动配置消息转换器:

@Configuration
@ConditionalOnClass(HttpMessageConverter.class)
@AutoConfigureAfter({GsonAutoConfiguration.class,
                          JacksonAutoConfiguration.class})
@Improt({JacksonHttpMessageConvertersConfiguration.class, 
            GsonHttpMessageConvertersConfiguration.class})
public class HttpMessageConverterAutoConfiguration{
    static final String PREFERRED_MAPPER_PROPERTY = 
            "spring.http.converters.preferred-json-mapper";
    private final List> converters;
}

 在这个类中定义了一个String类的转换器,其源码如下:

@Configuration
@ConditionalOnClass(StringHttpMessageConverter.class)
@EnableConfigurationProperties(HttpEncodingProperties.class)
protected static class StringHttpMessageConverterConfiguration{
    private final HttpEncodingProperties encodingProperties;

    protected StringHttpMessageConverterConfiguration(
                        HttpEncodingProperties encodingProperties){
        this.encodingProperties = encodingProperties;
    }

    @Bean
    @ConfigurationOnMissingBean
    public StringHttpMessageConverter StringHttpMessageConverter(){
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                    this.encodingProperties.getCharset());
        converter.setWriterAccpetCharset(false);
        return converter;
    }
}

注意:在默认的消息转换器中,使用的编码为UTF-8

2. 自定义消息转换器
 自定义消息换换器,我们只需要在配置类@Configuration的类中添加消息转换器的@Bean加入spring容器,就会被SpringBoot自动加入到容器中。
例如:

@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
    return converter;
}

3.自定义 MVC配置
有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。
 其代码如下:

import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{

    // 自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, 
                           HttpServletResponse response, Object handler)
                           throws Exception {
                System.out.println("自定义拦截器............");
                return true;
            }

            @Override
            public void postHandle(HttpServletRequest request, 
                        HttpServletResponse response, Object handler,
                    ModelAndView modelAndView) throws Exception {

            }

            @Override
            public void afterCompletion(HttpServletRequest request, 
                       HttpServletResponse response, Object handler,
                       Exception ex) throws Exception {
            }
        };
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

    // 自定义消息转化器的第二种方法
    @Override
    public void configureMessageConverters(
                    List> converters) {
        StringHttpMessageConverter converter  = 
                    new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

}

你可能感兴趣的:(springboot)