spring boot 规范json返回值
spring boot 接口返回配置 @ResponseBody ,则返回自定义的对象,解析成json。
但是,部分字段能友好的展示出来。如 Date,Long。Date必须手动格式化,才能输出想要的格式,还要注意时区问题。Long类型,相对过长的Long类型,会进行四舍五入等处理,失去准确性。
如Date类型处理需要给属性值加上注解:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
Long类型配置和其他配置,可以交给以下代码解决
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* web配置
*
* @author
* @date 2017-08-23 15:48
*/
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//设置日期格式
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
/**
* 序列换成json时,将所有的long变成string
* 因为js中得数字类型不能包含所有的java long值
*/
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
//设置中文编码格式
// List<MediaType> list = new ArrayList<MediaType>();
// list.add(MediaType.APPLICATION_JSON_UTF8);
// mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
return mappingJackson2HttpMessageConverter;
}
@Bean
public StringHttpMessageConverter getStringHttpMessageConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("GBK"));
//设置中文编码格式
List<MediaType> list = new ArrayList<MediaType>();
list.add(MediaType.APPLICATION_XML);
stringHttpMessageConverter.setSupportedMediaTypes(list);
return stringHttpMessageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
List<HttpMessageConverter<?>> convertersList = new HttpMessageConverters().getConverters();
for(int i = 0; i < convertersList.size(); i++){
if(convertersList.get(i) instanceof MappingJackson2HttpMessageConverter){
converters.add(getMappingJackson2HttpMessageConverter());
}else if(convertersList.get(i) instanceof StringHttpMessageConverter){
converters.add(getStringHttpMessageConverter());
}else{
converters.add(convertersList.get(i));
}
}
}
}