如何区分Java枚举是否存在或在Spring Boot中不正确地给出
有一个补丁方法可以更新项目的许多字段。 (可选)字段之一是状态(枚举)。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论