alibaba / alibaba/fastjson2

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

Open
#1,866 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

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

### 环境信息
- JDK信息: idea 自带jdk 11.0.12
- 版本信息:Fastjson2.0.40

### 期待结果
```json
{
"age": 0,
"attr": [
322233333333333
],
"id": 322223333333333,
"name": "hello"
}
```
> 1.2.38版本输出满足
### 实际结果
```json
{
"attr": [
322233333333333
],
"id": 322223333333333,
"name": "hello"
}
```

### 具体代码
#### controller
```java
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 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 getAttr() {
return attr;
}

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

}

```
### fastjson配置
```java

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 fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastConverter.setSupportedMediaTypes(fastMediaTypes);

fastConverter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastConverter);
}
}
```
### pom
```xml

com.alibaba.fastjson2
fastjson2
2.0.40


com.alibaba.fastjson2
fastjson2-extension-spring5
2.0.40

```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the shown TryController, User model, FastJsonConfig, and FastJsonHttpMessageConverter configuration using Fastjson 2.0.40, then compare against 1.2.83. Trace how @JSONField and WriteNullNumberAsZero are handled during Spring HTTP response serialization. Done means the /fastjson/serial response includes "age": 0 for a null Integer.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.