美文网首页
SpringCloud项目@ResponseBody不序列化值为

SpringCloud项目@ResponseBody不序列化值为

作者: 少泉一郎 | 来源:发表于2019-07-12 00:05 被阅读0次

    一、问题描述

      最近项目使用SpringCloud开发时,Json转换把属性值为null的属性过虑掉了。后续经过两小时的代码跟踪与调试才找出“真凶”。以下是本人根据当时的情况写的用例,如发现有不正确的地方,欢迎积极指正。

    实体Bean定义如下:

    public class Bean{
        private Integer id;
        private String name;
        private Date createDate;
    
        public Integer getId(){
            return id;
        }
        public void setId(Integer id){
            this.id = id;
        }
        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name = name;
        }
        public Date getCreateDate(){
            return createDate;
        }
        public void setCreateDate(Date createDate){
            this.createDate = createDate;
        }
    }
    

    代码片段如:

    @RestController
    @RequestMapping(value = "/api/")
    public class ConfigApi{
        private static final Logger logger = LoggerFactory.getLogger(ConfigApi.class);
        @Autowired
        private ObjectMapper objectMapper;
        
        @RequestMapping(value = "json", method = RequestMethod.GET)
        public @ResponseBody Bean rulePrefix(){
            try{
                Bean bean = new Bean();
                bean.setId(100);
                bean.setName(null);
                bean.setCreateDate(new Date());
                System.out.println(objectMapper.writeValueAsString(bean));
                return bean;
            }catch(Exception e){
                logger.error(e.getMessage(),e);
            }
        }
    }      
    

    期望转换后的json字符串应该为:

    {"id":100,"name":null,"createDate":1562858066864}

    实际转换的json为:

    {"id":100,"createDate":1562858066864}

    二、原因分析

      出现以上原因是由于定义ObjectMapper的时执行objectMapper.findAndRegisterModules();代码,这会把com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule注入,在以下jar包中。

    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jaxb-annotations</artifactId>
        <version>2.8.7</version>
    </dependency>
    

    依赖包如下:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.2.6.RELEASE</version>
    </dependency>
    

    ObjectMapper完整配置如下:

    @Configuration
    public class JacksonConfig{
        @Bean
        @Primary
        public ObjectMapper jsonMapper() {
            ObjectMapper objectMapper = JsonMapper.getInstance().getMapper();
            //注册所有Module接口的实现类
            objectMapper.findAndRegisterModules();
            //执行上面代码后,手动设置序列化方式都无效
            objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            return objectMapper;
        }
    
        @Bean
        @Primary
        public MappingJackson2HttpMessageConverter jsonConverter(){
            MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
            jsonConverter.setObjectMapper(jsonMapper());
            return jsonConverter;
        }
    }
    

    相关文章

      网友评论

          本文标题:SpringCloud项目@ResponseBody不序列化值为

          本文链接:https://www.haomeiwen.com/subject/trxskctx.html