zoukankan      html  css  js  c++  java
  • [心得体会]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());
    }

    }





  • 相关阅读:
    mvn command is not recognized as an internal or external command
    mac搭建nginx和wordpress开发环境
    搭建nginx+php环境时遇到”file not found"的问题
    MAC下bash配置文件的加载顺序
    lsof的注意事项
    理解字符集、字符编码、ASCII、Unicode、UTF-8
    初学markdown有感
    AngularJS开发指南-Animations
    #正则表达式# 学习笔记
    vi学习笔记/基本操作方法
  • 原文地址:https://www.cnblogs.com/bangiao/p/12418366.html
Copyright © 2011-2022 走看看