1. 自動配置的消息轉換器
在SptingBoot的源碼中的spring-boot-autoconfig的Jar包下,咱們能夠找到org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.class文件,這個類就是自動配置消息轉換器:java
@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<HttpMessageConverter<?>> converters;
}
1
2
3
4
5
6
7
8
9
10
11
在這個類中定義了一個String類的轉換器,其源碼以下:web
@Configuration
@ConditionalOnClass(StringHttpMessageConverter.class)
@EnableConfigurationProperties(HttpEncodingProperties.class)
protected static class StringHttpMessageConverterConfiguration{
private final HttpEncodingProperties encodingProperties;spring
protected StringHttpMessageConverterConfiguration(
HttpEncodingProperties encodingProperties){
this.encodingProperties = encodingProperties;
}json
@Bean
@ConfigurationOnMissingBean
public StringHttpMessageConverter StringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(
this.encodingProperties.getCharset());
converter.setWriterAccpetCharset(false);
return converter;
}
}
注意:在默認的消息轉換器中,使用的編碼爲UTF-8。app
2. 自定義消息轉換器
自定義消息換換器,咱們只須要在配置類@Configuration的類中添加消息轉換器的@Bean加入spring容器,就會被SpringBoot自動加入到容器中。
例如:ide
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
}
3.自定義 MVC配置
有些時候咱們須要自已配置SpringMVC而不是採用默認,好比說增長一個攔截器,這個時候就得經過繼承WebMvcConfigurerAdapter而後重寫父類中的方法進行擴展。
其代碼以下:spring-boot
import java.nio.charset.Charset;
import java.util.List;post
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;this
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<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter converter =
new StringHttpMessageConverter(Charset.forName("UTF-8"));
converters.add(converter);
}
}