如何解决Springboot:如何在任何类型失败的情况下覆盖默认的 JSON 响应结构?
在我的 Springboot(2.x.x) 应用程序中,在使用 REST
控制器时,我需要处理两种类型的故障
-
自定义验证失败,在这里我可以根据需要生成任何 Json 响应结构(即我需要的任何字段)
-
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
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。