java - Springboot:如何在任何类型失败的情况下覆盖默认的 JSON 响应结构?
问题描述
在我的 Springboot(2.xx) 应用程序中,在使用REST
Controller 时,我需要处理两种类型的故障
自定义验证失败,这里我可以根据需要生成任何 Json Response 结构(即我需要的任何字段)
Springboot 的默认验证失败(下面给出的几个示例以及更多示例)。在这里,它生成 5xx 或 4xx 以及 json 响应中的默认几个字段。如何在应用层覆盖它并匹配我的自定义失败 JSON 响应?
MissingServletRequestPartException.class, //it will be thrown when one of form param is missing
MissingServletRequestParameterException.class, //it will be thrown when one of request param is missing
MethodArgumentNotValidException.class, //it will be thrown when form json param value is not valid
MethodArgumentTypeMismatchException.class, //it will be thrown when request param type value is mismatched
ConstraintViolationException.class, // it will be thrown when any of request param is not valid
解决方案
推荐阅读
- javascript - How to stop observers in Observable.merge?
- python - 从 DateTime 中删除时区 (+01:00)
- c# - 如何从静态方法内部更新控件(TextBlock)
- r - 获取一行中的第一个非 NA 元素
- python - 千位分隔符以正则表达式的下一个单词为条件
- python - 如何为 pandas 日期时间转换添加开始日期并将公式扩展到所有行,如 excel 中的?
- reactjs - 用 jest 和酶窥探 React 功能组件方法;无法窥探原始值
- python - Django CKEDITOR,图像上传路径与 ImageField 上传路径不同
- html - 使 div 使用父级的整个宽度,而不增加它
- git - 如何在 fork 和 master 分支之间发生 git 冲突