com.iiifi.kite.boot.web.servlet.config.KiteMessageConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kite-boot Show documentation
Show all versions of kite-boot Show documentation
Spring Cloud Core Component Extension.
package com.iiifi.kite.boot.web.servlet.config;
import java.util.List;
import lombok.AllArgsConstructor;
import java.nio.charset.StandardCharsets;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iiifi.kite.boot.properties.KiteJacksonProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.iiifi.kite.common.jackson.MappingApiJackson2HttpMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* 消息转换器
* @author [email protected] 花朝
* @date 2018/8/18 13:46
*/
@Configuration
@AllArgsConstructor
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class KiteMessageConfiguration implements WebMvcConfigurer {
private final KiteJacksonProperties properties;
private final ObjectMapper objectMapper;
/**
* 消息转换,内置断点续传,下载和字符串
*
* @param converters 转换器
*/
@Override
public void configureMessageConverters(List> converters) {
converters.removeIf(x -> x instanceof StringHttpMessageConverter || x instanceof AbstractJackson2HttpMessageConverter);
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
// 如果需要将 null 转为 空
if (properties.getNullToEmpty()) {
converters.add(new MappingApiJackson2HttpMessageConverter(objectMapper));
} else {
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy