使用自定义消息捕获和处理 Jackson 异常

2023-12-30

我希望能够捕获我正在开发的 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(使用前将#替换为@)

使用自定义消息捕获和处理 Jackson 异常 的相关文章

随机推荐

  • libsandbox 和 pysandbox 的限制功能

    我正在使用sample2 py此处列出https github com openjudge sandbox https github com openjudge sandbox作为调用沙箱库的包装器 又名 libsandbox pysand
  • 如何使用 R Shiny 从数据库填充下拉列表?

    我想用数据库查询的结果填充 R 闪亮应用程序中的下拉列表 I have a global r file where i put this code getData lt function this function returns a si
  • 停止 Android Studio 使用支持库

    我试图让 Android Studio 中的 Android 项目不使用支持库 我的问题是 当我使用片段时 它需要支持片段并导致应用程序崩溃 我的 Min SDK 是 14 我的 Target SDK 是 19 我的理解是 因为我的目标是这
  • Flask + Flask-Security + Babel 不工作

    我已经设置了 Flask Babel Flask Security 像这样创建所有翻译 root main py translations ru LC MESSAGES 消息 mo 消息 po 在 main py 中 有一部分设置语言执行
  • Struts2:全局结果配置错误

    我想使用全局结果 登录 但它仅在配置中没有全局结果的情况下才有效 这是我的配置的相关部分 类别列表 jsp 类别详细信息 jsp WEB INF login jsp 当我启动我的应用程序时 它会抛出此异常 org xml sax SAXPa
  • 如何将文本文件包含到JavaScript中

    有没有办法将另一个文件中的一些文本加载到 javascript 中 而不需要服务器端代码 我正在考虑使用另一个元素来保存一些注释中的文本 但我不知道如何使用 javascript 读取它的源代码 就像是 在 myfile js 中 some
  • 重载 << 运算符 C++ - 指向类的指针

    class logger logger operator lt lt logger log const std string str cout lt lt My Log lt lt str lt lt endl return log log
  • iOS 中的 Amazon S3 POST 上传

    我有一个服务器 它生成 AWSAccessKeyID acl 策略 签名等参数 用于使用 POST 将文件上传到 S3 如下所示 http doc s3 amazonaws com proposals post html http doc
  • 如何在 .NET 4.0 的实体框架中进行“like”通配符比较?

    我正在使用 Visual Studio 2010 RC for NET 4 0 我试图弄清楚如何与实体框架进行通配符比较 我想要对 EF 进行以下查询 在其中找到所有以 J 开头的名称 select from Users where Fir
  • Cloud Dataflow - 增加 JVM Xmx 值

    我们正在尝试在云中运行 Google Cloud Dataflow 作业 但我们不断收到 java lang OutOfMemoryError Java 堆空间 我们正在尝试处理 Big Query 表中的 6 1 亿条记录 并将处理后的记
  • Mapbox fitbounds() - 无效的 LngLat 对象:(NaN, NaN)

    在过去的几个小时里 我一直用头撞桌子 我试图让 Mapbox 放大所有标记的边界区域的负载 但是 这是我在下面的代码中遇到的错误 此错误出现在下面的控制台日志图像之后 因此 lat lng 坐标肯定存在 未捕获的错误 无效的 LngLat
  • 如果仓库自动查看自述文件,自述文件中的相对链接就会损坏

    我有一个仓库作为 github 上组织的一部分 例如 MYORGANIZATION MyRepository 该存储库包含一个 README md 文件 在此 README md 文件中 我有一个链接 因此另一个 md 文件位于与 READ
  • Xampp-mysql - “引擎中不存在表”#1932

    移动 xampp 文件夹后出现 Xampp 错误 Apache 运行良好 如下图所示 我成功运行 apache 但无法运行 mysql 如果我移动并运行 xampp apache 和 mysql 但它显示的错误是新鲜的 并且显示类似 引擎中
  • 将 exec-maven-plugin 的输出分配给变量

    我想使用 exec maven plugin 来获取 git revision 所以我使用以下配置
  • 用于执行 python 脚本的 Vim 键映射

    我第一次尝试在 Vim 中映射命令 我希望它执行当前的 python 脚本 所以我将以下内容放入我的 vimrc noremap
  • 添加 $ 并舍入小数点后 2 位 SQL

    我正在寻找格式化由子查询创建的输出 该子查询生成一个计算字段 我希望将其格式化为 XX XX 子查询 select avg retail from cars where brand FORD or brand TOYOTA as AVG B
  • 使用 ICC 配置文件将 RGB 转换为 CMYK

    我即将将 RGB 颜色转换为 CMYK 以便打印 此转换的规模是 Adob e Photoshop Image gt Mode gt CMYK color 我尝试了 2 个解决方案 但没有一个返回正确的值 解决方案 1 使用 NET Fra
  • webpack --watch 不编译更改的文件

    我尝试跑步webpack watch编辑我的 JS 文件后 它不会触发自动重新编译 我尝试过重新安装webpack using npm uninstall但它仍然不起作用 有任何想法吗 如果您的代码没有被重新编译 请尝试增加观察者的数量 在
  • 何时使用后值[重复]

    这个问题在这里已经有答案了 在下面的一段代码中 viewModelScope launch isLoading postValue true 我们在主线程中 因为viewModelScope launch默认在主线程上运行 与viewMod
  • 使用自定义消息捕获和处理 Jackson 异常

    我希望能够捕获我正在开发的 spring boot API 中发生的一些 Jackson 异常 例如 我有以下请求类 我想捕获当 JSON 请求对象中的 questionnaireResponse 键为 null 或空白时发生的错误 即 在