2019-07-24

1724 단어
/**
 * mvc   
 * @author: nss
 * @date: 2018/10/17 20:17
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {


    private final ObjectMapper mapper;

    @Autowired
    public MvcConfig(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void configureMessageConverters(List> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //            
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //       
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        //       
        mapper.configure(SerializationFeature.INDENT_OUTPUT,true);
        //      json       ,          “No serializer found for class com.xxx.xxx”
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,true);
        //   json ,    long  string
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        mapper.registerModule(simpleModule);
        //   null     
        //Todo   
        //mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        jackson2HttpMessageConverter.setObjectMapper(mapper);
        converters.add(jackson2HttpMessageConverter);
        super.configureMessageConverters(converters);
    }

}

좋은 웹페이지 즐겨찾기