• [心得体会]springmvc在requestbody注解下使用jackson转化日期格式


    使用WebMvcConfigurer的方法将converter注入到项目中
    @Configuration
    public class ConverterConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/pages/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/js/**", "/images/**", "/css/**").addResourceLocations("/js/", "/images/", "/css/").resourceChain(true).addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
    }

    /**
    * jacksonjson转化dateconverter接口
    *
    * @return
    */
    // @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
    ObjectMapper objectMapper = jackson2ObjectMapperBuilder.build();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    DateFormat dateFormat = objectMapper.getDateFormat();
    objectMapper.setDateFormat(new MyDateFormat(dateFormat));
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    List<MediaType> list = new ArrayList<>();
    list.add(MediaType.APPLICATION_JSON);
    mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
    return mappingJackson2HttpMessageConverter;
    }

    /**
    * 增加到我们配置内部
    *
    * @param converters
    */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(mappingJackson2HttpMessageConverter());
    }

    }





  • 相关阅读:
    使用Git遇到的问题及解决方案(初级)
    巧记前端易混淆的英文语法
    编程助手JavaScript学习库-DOM笔记
    编程助手JavaScript学习库-面向对象编程笔记
    JS原始类型:数值的运用技巧
    这题目有毒之干不过codewars的OJ系统(一)
    Javascript运算符进阶指南
    Qt:During startup program exited with code 0xc0000135
    QT 消息处理机制
    Windows环境 和 Linux环境下搭建Qt开发环境
  • 原文地址:https://www.cnblogs.com/bangiao/p/12418366.html
Copyright © 2020-2023  润新知