Jackson 2.16 原始数据类型的自定义序列化程序。您应该编写自己的序列化程序。布尔数据类型的示例代码
// 创建一个带有自定义布尔序列化器的模块
class BooleanSerializer extends JsonSerializer<Boolean> {
private final static Logger logger = LoggerFactory.getLogger(BooleanSerializer.class);
@Override
public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
logger.info("serializing boolean value as a Strng {}",value);
jgen.writeString(value.toString());
}
}
//使用 ObjectMapper 注册自定义 BooleanSerializer 类。
// 这里是我们配置对象映射器的地方
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("BooleanAsString", new Version(1, 0, 0, null, null, null));
simpleModule.addSerializer(Boolean.class,new BooleanSerializer());
simpleModule.addSerializer(boolean.class,new BooleanSerializer());
mapper.registerModule(module);