Spring Boot HttpMessageConverter 配置

在没有Spring Boot的时候,Spring MVC的HttpMessageConverter配置一般都是在xml中,现在没有了xml,只能在class中配置,代码如下:

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;


@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {


@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new FastJsonHttpMessageConverter4());
//全局控制,解决当对象存在引用时,序列化后的结果浏览器不支持的问题。
JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.DisableCircularReferenceDetect.getMask();
//局部控制
//JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
}

}

除了配置HttpMessageConverter,还可以覆盖其他方法进行一些配置,例如addResourceHandlers方法和addViewControllers。

import java.util.List;

import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
// registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new FastJsonHttpMessageConverter4());
//全局控制,解决当对象存在引用时,序列化后的结果浏览器不支持的问题。
JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.DisableCircularReferenceDetect.getMask();
//局部控制
//JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/welcome").setViewName("welcome");
registry.addViewController("/login-page").setViewName("login");
registry.addViewController("/logout/success").setViewName("logout_success");
//
registry.addRedirectViewController("/", "/welcome");
}

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
ErrorPage error401Page = new ErrorPage(HttpStatus.FORBIDDEN, "/WEB-INF/views/403.jsp");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/WEB-INF/views/404.jsp");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/WEB-INF/views/500.jsp");
container.addErrorPages(error401Page, error404Page, error500Page);
});
}

}

还有更多可以配置的选项。。。

分享到