alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.81k stars 498 forks source link

[BUG] 使用JSON.toJSONString(),输出org.springframework.cloud.client.ServiceInstance的JSON 报错 #2621

Open Jay2293 opened 6 months ago

Jay2293 commented 6 months ago

问题描述

使用JSON.toJSONString(),输出org.springframework.cloud.client.ServiceInstance的JSON 报错

环境信息

重现步骤

使用以下代码可以实现:

  1. 使用 JSON.toJSONString(),输出org.springframework.cloud.client.ServiceInstance的JSON` 方法
  2. 输入 报错 数据
  3. 出现 can't get field offset on a record class: private final boolean org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance.secure at jdk.unsupported/sun.misc.Unsafe.objectFieldOffset(Unsafe.java:655) at com.alibaba.fastjson2.writer.FieldWriter.<init>(FieldWriter.java:116)
import com.alibaba.fastjson2.JSON;
import com.nitxe.western.common.type.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
public class HelloWorldController {

    @GetMapping("/hello-world")
    public Result<List<ServiceInstance>> helloWorld() {

        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);

        if (instances.isEmpty()) {
            return Result.fail("No online services available.");
        }

        log.info("Calling Hello World Interface Success Result Is {}", JSON.toJSONString(instances));

        return Result.ok(instances);
    }

    @Value("${spring.application.name}")
    private String serviceName;

    private final DiscoveryClient discoveryClient;
}

期待的正确结果

正常输出json

相关日志输出

wenshao commented 6 months ago

org.springframework.cloud.client.ServiceInstance 这类在哪个Maven坐标下,找不到无法重现

Jay2293 commented 6 months ago

org.springframework.cloud.client.ServiceInstance 蚂蚁在哪个Maven坐标下,找不到无法重现

我使用的是 spring boot 3.2.5 maven坐标是 有这个依赖引入的

org.springframework.cloud spring-cloud-starter-kubernetes-fabric8-all 3.1.0

他自动引入的 具体版本是 3.1.0

hnyyghk commented 5 months ago

this is a simple unit test, will cause UnsupportedOperationException("can't get field offset on a record class: " + f)

TestRecord testRecord = new TestRecord("123");
log.info(com.alibaba.fastjson2.JSON.toJSONString(testRecord));

public record TestRecord(String instanceId) {
    public String getInstanceId() {
        return instanceId;
    }
}

image

ityangkai-coder commented 1 month ago

org.springframework.cloud.client.ServiceInstance 这类在哪个Maven坐标下,找不到无法重现

我认为和Spring无关,针对record class的boolean属性都会出现这个问题,可以参考一下我提出的问题:https://github.com/alibaba/fastjson2/issues/3090

使用最基础的boolean就会出问题,fastjson2针对boolean有特殊的处理,导致问题出现,同样使用Boolean就不会,麻烦尽快修复一下