这是我的自定义 JSON 序列化器,我正在尝试将其注册为模块。这是我到目前为止所拥有的。
public class MaptoListSerializer extends JsonSerializer<Map<String, Car<?, ?>>> {
@Override
public void serialize(Map<String, Car<?, ?>> value, JsonGenerator gen, SerializerProvider serializers)throws IOException, JsonProcessingException {
gen.writeObject(value.values());
}
}
这是我创建的模块类(似乎无法添加序列化程序)。
public class CustomModule extends SimpleModule {
private static final long serialVersionUID = -9105685985325373621L;
public CustomModule() {
super("CustomModule");
}
@Override
public void setupModule(SetupContext context) {
SimpleSerializers serializers = new SimpleSerializers();
//THIS DOESN'T WORK . HOW DO I ADD THE SERIALIZER?
serializers.addSerializer(HashMap.class, new MaptoListSerializer ());
context.addSerializers(serializers);
}
}
这是它使用的对象映射器的方式(这有效)
mapper = new ObjectMapper();
mapper.registerModule(new CustomModule());