Как я могу сериализовать свойства как объект json? - PullRequest
0 голосов
/ 16 октября 2018

Как я могу сериализовать следующий общий ответ, используя Джексона ??

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"
            }
    }
}

Как я могу получить это с помощью Джексона ???

Если я установил значение wrap_root_value в true, оно будет сериализовано в следующем формате ...

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

1 Ответ

0 голосов
/ 17 октября 2018

Я могу получить это, используя аннотации @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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...