Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] WriteNullNumberAsZero配合springboot框架不起效 #1866

Open
SiyuanLi-code opened this issue Sep 14, 2023 · 1 comment
Open

[BUG] WriteNullNumberAsZero配合springboot框架不起效 #1866

SiyuanLi-code opened this issue Sep 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@SiyuanLi-code
Copy link

问题描述

配置了fastjson的FastJsonHttpMessageConverter,@JSONField的serialzeFeatures,WriteNullNumberAsZero,不起效。经过验证,在1.2.83版本配置内能正常序列化,升级到2.0.40后,无效

环境信息

  • JDK信息: idea 自带jdk 11.0.12
  • 版本信息:Fastjson2.0.40

期待结果

{
  "age": 0,
  "attr": [
    322233333333333
  ],
  "id": 322223333333333,
  "name": "hello"
}

1.2.38版本输出满足

实际结果

{
  "attr": [
    322233333333333
  ],
  "id": 322223333333333,
  "name": "hello"
}

具体代码

controller

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/fastjson")
public class TryController {

    @GetMapping("/serial")
    public User getOne(){
        User hello = new User();
        hello.setAge(null);
        hello.setName("hello");
        hello.setId(322223333333333L);
        hello.setAttr(ListUtil.list(false, 322233333333333L));
        return hello;
    }

    public static class User {

        @JSONField(serializeFeatures = JSONWriter.Feature.WriteNullNumberAsZero)
        private Integer age;

        private Long id;

        private String name;

        private List<Long> attr;

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Long> getAttr() {
            return attr;
        }

        public void setAttr(List<Long> attr) {
            this.attr = attr;
        }
    }

}

fastjson配置

import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FastjsonConfig {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }
}

pom

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.40</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2-extension-spring5</artifactId>
            <version>2.0.40</version>
        </dependency>
@SiyuanLi-code SiyuanLi-code added the bug Something isn't working label Sep 14, 2023
@liaozan
Copy link
Contributor

liaozan commented Nov 1, 2023

试下最新版本?可能 #1898 解决了这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants