alibaba / fastjson2

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

[QUESTION] 咨询一个关于泛型枚举 Enum 中使用 @JSONField(value = true) GraalVM不生效,需要哪些配置么? #1824

Open niaoshuai opened 1 year ago

niaoshuai commented 1 year ago

请描述您的问题

咨询一个关于泛型枚举中使用 @JSONField(value = true) 本地调试可以使用 使用 GraalVM编译启动不生效,需要哪些配置么?

public interface IEnum<T> {

    @JSONField(value = true)
    T getValue();
}

从这个里面找到资料最后调整的配置

https://github.com/alibaba/fastjson2/issues/1501

目前临时的解决办法是 在子枚举中 每一个 getValue 配置了一下 @JSONField(value = true)

wenshao commented 1 year ago

什么版本?能提供重现问题的testcase么?

niaoshuai commented 1 year ago

使用的版本号是 2.0.39,springboot 3.1.2 项目直接启动没问题,只要编译成graalvm就不识别枚举了


public class FastJson2Writer implements HttpMessageWriter<Object> {
    @Override
    @NonNull
    public List<MediaType> getWritableMediaTypes() {
        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.APPLICATION_JSON);
        return mediaTypeList;
    }

    @Override
    public boolean canWrite(@NonNull ResolvableType elementType, MediaType mediaType) {
        return (mediaType == null || MediaType.APPLICATION_JSON.includes(mediaType));
    }

    @Override
    @NonNull
    public Mono<Void> write(@NonNull Publisher<?> inputStream, @NonNull ResolvableType elementType, MediaType mediaType, @NonNull ReactiveHttpOutputMessage message, @NonNull Map<String, Object> hints) {
        return Mono.from(inputStream).flatMap(is -> {
            message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            DataBuffer dataBuffer = message.bufferFactory().wrap(JSON.toJSONBytes(is, JSONWriter.Feature.WriteNullStringAsEmpty));
            Flux<DataBuffer> dataStream = Flux.just(dataBuffer);
            return message.writeWith(dataStream);
        });
    }
}
配置类

@Configuration(proxyBeanMethods = false)
public class FastJson2Config {

    @Bean
    public CodecCustomizer fastjson2CodecCustomizer() {
        return (configurer) -> {
//            configurer.registerDefaults(false);
//            configurer.customCodecs().register(new FastJson2Reader());
            configurer.customCodecs().register(new FastJson2Writer());
        };
    }
}
niaoshuai commented 1 year ago

刚看到 2.0.40升级了,我测试下,

niaoshuai commented 1 year ago

2.0.40也不行,

import com.alibaba.fastjson2.annotation.JSONField;

public interface IEnum<T> {

    @JSONField(value = true)
    T getValue();
}

@AllArgsConstructor
public enum RetEnums implements IEnum<String> {
    SUCCESS("0", "OK"),
    LOGIN_FAILED("-10020", "登录失败"),
    FILE_UPLOAD_ERROR("-10000", "文件上传失败");

    final String code;
    final String defaultErrMsg;

    public String getValue() {
        return this.code;
    }

}
{
    "code": "LOGIN_FAILED",
    "errMsg": "Token无效:8ef1ce37-8097-4b99-aeec-2e55e3881b24"
}
niaoshuai commented 1 year ago

临时办法,是在集成泛型枚举的下面的所有子枚举 getValue 都配置 @JSONField(value = true)

   @JSONField(value = true)
    public String getValue() {
        return this.code;
    }