如何将属性序列化为 json 对象?

How can I serialize properties as json object?

如何使用 jackson 序列化以下通用响应?

public class GenericResponse{
    private String resource;
    private Integer status;
    private ErrorInfo response;
    //setters and getters
}

public class ErrorInfo {
    private String errorCode;
    private String errorDetails;
    @JsonUnwrapped
    private ErrorFactory errorFactory;
    //setters and getters
}

预期输出:

{
    "resource": "xxxxxx",
    "status": xxxxx,
    "response": {
        "error-info": {
            "errorCode": "xxxxxx",
            "errorDetails": "xxxxx"
            }
    }
}

我如何使用 jackson 得到这个???

如果我将 wrap_root_value 设置为 true,那么它将以以下格式序列化....

{
    "GenericResponse": {
        "resource": "xxxxxx",
        "status": xxxxxxxxx,
        "response": {
            "errorCode": "xxxxxxxxx",
            "errorDetails": "xxxxxxxxxxx"
        }
    }
}

我可以通过使用 @JsonTypeInfo@JsonTypeName 注释来获得它。

public class GenericResponse{
private String resource;
private Integer status;
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
private ErrorInfo response;
//setters and getters
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonTypeName("error-info")
public class ErrorInfo {
private String errorCode;
private String errorDetails;
@JsonUnwrapped
private ErrorFactory errorFactory;
}