【发布时间】:2021-07-12 16:48:54
【问题描述】:
在我的 Springboot(2.x.x) 应用程序中,在使用 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
【问题讨论】:
标签: java json spring spring-boot spring-restcontroller