我想在Jackson序列化器中序列化子对象。
所以我有一个像这样的对象:
@JsonSerialize(using = StateSerializer.class)
public class State {
...
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonProperty(value = "d", required = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonSerialize(using = StateDefinitionSerializer.class)
private StateDefinition definition;
...
}
在我的StateSerializer中我想使用自己的名为StateDefinitionSerializer的seriliazer序列化我的StateDefinition:
public class StateSerializer extends JsonSerializer<State> {
@Override
public void serialize(State state, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
...
jsonGenerator.writeObject(state.getDefinition());
但后来我最终在jackson DefaultSerializerProvider中获得了一个IOException:
} catch (IOException ioe) { // As per [JACKSON-99], pass IOException and subtypes as-is
throw ioe;
}
关于如何解决这个问题的任何想法? :)
所有帮助/评论都得到了很多赞赏。
祝福,
约亨
答案 0 :(得分:0)
你需要将@JsonSerialize的注释(使用= StateSerializer.class)放在类的上方,但是要在序列化的对象或参数之上。
在您的情况下,您可以这样做:
@JsonSerialize(using = StateSerializer.class)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
...
private StateDefinition definition;