You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Jackson在Java中序列化AstronomicalBody接口实现类时带上接口名称?

方案1:生成带"AstronomicalBody"根键的JSON

要实现这种外层包裹根键的格式,有两种可行方式:

方式A:@JsonRootName + 全局配置

给AstronomicalBody接口添加@JsonRootName注解,同时在Jackson的ObjectMapper中启用WRAP_ROOT_VALUE序列化特性:

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@JsonRootName("AstronomicalBody")
public interface AstronomicalBody {}

public class Planet implements AstronomicalBody {
    private String name;
    private double mass;
    // 构造器、getter/setter省略
}

// 序列化逻辑
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

Planet earth = new Planet("Earth", 5.972e24);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(earth);

生成的JSON格式如下:

{
  "AstronomicalBody" : {
    "name" : "Earth",
    "mass" : 5.972e24
  }
}

方式B:自定义序列化器(局部生效)

如果全局配置会影响其他类的序列化行为,可以给AstronomicalBody的实现类单独指定自定义序列化器:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;

public interface AstronomicalBody {}

@JsonSerialize(using = AstronomicalBodyWrapperSerializer.class)
public class Star implements AstronomicalBody {
    private String name;
    private int temperature;
    // 构造器、getter/setter省略
}

public class AstronomicalBodyWrapperSerializer extends JsonSerializer<AstronomicalBody> {
    @Override
    public void serialize(AstronomicalBody value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeObjectField("AstronomicalBody", value);
        gen.writeEndObject();
    }
}

这种方式仅对标记了@JsonSerialize注解的类生效,不会干扰全局配置。


方案2:生成带"entity"和"details"字段的JSON

这种结构需要通过自定义序列化器来包装对象,具体实现如下:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;

public interface AstronomicalBody {}

// 可将该注解直接加在AstronomicalBody接口上,让所有实现类自动生效
@JsonSerialize(using = EntityDetailsWrapperSerializer.class)
public class Planet implements AstronomicalBody {
    private String name;
    private double mass;
    // 构造器、getter/setter省略
}

public class EntityDetailsWrapperSerializer extends JsonSerializer<AstronomicalBody> {
    @Override
    public void serialize(AstronomicalBody value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("entity", "AstronomicalBody");
        gen.writeObjectField("details", value);
        gen.writeEndObject();
    }
}

// 序列化逻辑
ObjectMapper mapper = new ObjectMapper();
Planet mars = new Planet("Mars", 6.39e23);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mars);

生成的JSON格式如下:

{
  "entity" : "AstronomicalBody",
  "details" : {
    "name" : "Mars",
    "mass" : 6.39e23
  }
}

内容的提问来源于stack exchange,提问作者user24030568

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:03:25