使用自定义消息捕获和处理 Jackson 异常
2022-09-02 14:03:33
我希望能够发现一些杰克逊异常,这些异常发生在我正在开发的spring-boot API中。例如,我有以下请求类,并且我想捕获当JSON请求对象中的“问卷响应”键为空或空白(即请求对象)时发生的错误。" "
@Validated
@JsonRootName("questionnaireResponse")
public class QuestionnaireResponse {
@JsonProperty("identifier")
@Valid
private Identifier identifier = null;
@JsonProperty("basedOn")
@Valid
private List<Identifier_WRAPPED> basedOn = null;
@JsonProperty("parent")
@Valid
private List<Identifier_WRAPPED> parent = null;
@JsonProperty("questionnaire")
@NotNull(message = "40000")
@Valid
private Identifier_WRAPPED questionnaire = null;
@JsonProperty("status")
@NotNull(message = "40000")
@NotEmptyString(message = "40005")
private String status = null;
@JsonProperty("subject")
@Valid
private Identifier_WRAPPED subject = null;
@JsonProperty("context")
@Valid
private Identifier_WRAPPED context = null;
@JsonProperty("authored")
@NotNull(message = "40000")
@NotEmptyString(message = "40005")
@Pattern(regexp = "\\d{4}-(?:0[1-9]|[1-2]\\d|3[0-1])-(?:0[1-9]|1[0-2])T(?:[0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\dZ", message = "40001")
private String authored;
@JsonProperty("author")
@NotNull(message = "40000")
@Valid
private QuestionnaireResponseAuthor author = null;
@JsonProperty("source")
@NotNull(message = "40000")
@Valid
private Identifier_WRAPPED source = null; // Reference(Patient | Practitioner | RelatedPerson) resources not implemented
@JsonProperty("item")
@NotNull(message = "40000")
@Valid
private List<QuestionnaireResponseItem> item = null;
public Identifier getIdentifier() {
return identifier;
}
public void setIdentifier(Identifier identifier) {
this.identifier = identifier;
}
public List<Identifier_WRAPPED> getBasedOn() {
return basedOn;
}
public void setBasedOn(List<Identifier_WRAPPED> basedOn) {
this.basedOn = basedOn;
}
public List<Identifier_WRAPPED> getParent() {
return parent;
}
public void setParent(List<Identifier_WRAPPED> parent) {
this.parent = parent;
}
public Identifier_WRAPPED getQuestionnaire() {
return questionnaire;
}
public void setQuestionnaire(Identifier_WRAPPED questionnaire) {
this.questionnaire = questionnaire;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Identifier_WRAPPED getSubject() {
return subject;
}
public void setSubject(Identifier_WRAPPED subject) {
this.subject = subject;
}
public Identifier_WRAPPED getContext() {
return context;
}
public void setContext(Identifier_WRAPPED context) {
this.context = context;
}
public String getAuthored() {
return authored;
}
public void setAuthored(String authored) {
this.authored = authored;
}
public QuestionnaireResponseAuthor getAuthor() {
return author;
}
public void setAuthor(QuestionnaireResponseAuthor author) {
this.author = author;
}
public Identifier_WRAPPED getSource() {
return source;
}
public void setSource(Identifier_WRAPPED source) {
this.source = source;
}
public List<QuestionnaireResponseItem> getItem() {
return item;
}
public void setItem(List<QuestionnaireResponseItem> item) {
this.item = item;
}
}
导致此杰克逊错误:
{
"Map": {
"timestamp": "2018-07-25T12:45:32.285Z",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Root name '' does not match expected ('questionnaireResponse') for type [simple type, class com.optum.genomix.model.gel.QuestionnaireResponse]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name '' does not match expected ('questionnaireResponse') for type [simple type, class com.optum.genomix.model.gel.QuestionnaireResponse]\n at [Source: (PushbackInputStream); line: 2, column: 3]",
"path": "/api/optumhealth/genomics/v1.0/questionnaireResponse/create"
}
}
有没有办法捕获和处理这些异常(在示例JsonRootName中为空/无效),也许类似于扩展ReactEntityExceptionHandler@ControllerAdvice类?