註解完成spring json返回數據格式配置

import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.text.SimpleDateFormat;import java.util.List;/** * @author mahuan * @version 2018/04/12 **/@EnableWebMvc@Configurationpublic class JsonConfig extends WebMvcConfigurerAdapter {    @Override    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));        SimpleModule simpleModule = new SimpleModule();        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);        objectMapper.registerModule(simpleModule);        jackson2HttpMessageConverter.setObjectMapper(objectMapper);        //放到第一個        converters.add(0, jackson2HttpMessageConverter);    }}
相關文章
相關標籤/搜索