我希望能够捕获我正在开发的 spring-boot API 中发生的一些 Jackson 异常。例如,我有以下请求类,我想捕获当 JSON 请求对象中的“questionnaireResponse”键为 null 或空白时发生的错误,即" "
在请求对象中。
@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 为 null/无效),也许类似于扩展 ResponseEntityExceptionHandler 的 @ControllerAdvice 类?
尝试一些类似的事情:
@ControllerAdvice
public class ExceptionConfiguration extends ResponseEntityExceptionHandler {
@ExceptionHandler(JsonMappingException.class) // Or whatever exception type you want to handle
public ResponseEntity<SomeErrorResponsePojo> handleConverterErrors(JsonMappingException exception) { // Or whatever exception type you want to handle
return ResponseEntity.status(...).body(...your response pojo...).build();
}
}
它允许您处理任何类型的异常并做出相应的响应。如果响应状态始终相同,只需粘贴@ResponseStatus(HttpStatus.some_status)
关于方法和调用ResponseEntity.body(...)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)