如何区分Java枚举是否存在或在Spring Boot中不正确地给出

发布于 2025-01-23 06:30:07 字数 655 浏览 1 评论 0原文

有一个补丁方法可以更新项目的许多字段。 (可选)字段之一是状态(枚举)。

public class ItemUpdate {
    // ...
    public enum Status {
        A("a"),
        B("b");
        // ...
    }
}

但是,我希望能够区分枚举是否被错误地给出(例如“ C”),或者甚至没有在请求正文中发送。在这两种情况下,log.info()写入null。

@RequestMapping(
    value = "/item/{id}",
    method = RequestMethod.PATCH
)
public ResponseEntity<Item> patchItem(
    @PathVariable("id") String id, 
    @RequestBody ItemUpdate itemUpdate
) {
    log.info( String.format("status enum = %s", itemUpdate.getStatus()) );
    // ...
}

There's a PATCH method that updates many fields of an item. One of the (optional) fields is the status (an Enum).

public class ItemUpdate {
    // ...
    public enum Status {
        A("a"),
        B("b");
        // ...
    }
}

However I want to be able to distinguish if the Enum was incorrectly given (e.g. "c") or it wasn't even sent in the request body. In both scenarios the log.info() writes null.

@RequestMapping(
    value = "/item/{id}",
    method = RequestMethod.PATCH
)
public ResponseEntity<Item> patchItem(
    @PathVariable("id") String id, 
    @RequestBody ItemUpdate itemUpdate
) {
    log.info( String.format("status enum = %s", itemUpdate.getStatus()) );
    // ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文