@RestControllerAdvice
@ConditionalOnProperty(prefix="cc-spring.web",
name="enable-default-advice",
havingValue="true",
matchIfMissing=true)
public class ControllerResponseBodyAdvice
extends Object
implements org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>
| Constructor and Description |
|---|
ControllerResponseBodyAdvice() |
| Modifier and Type | Method and Description |
|---|---|
Object |
beforeBodyWrite(Object body,
org.springframework.core.MethodParameter returnType,
org.springframework.http.MediaType selectedContentType,
Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType,
org.springframework.http.server.ServerHttpRequest request,
org.springframework.http.server.ServerHttpResponse response)
返回结果统一处理
对于加了 @ResultAnnotation 注解的方法 将你的结果组装成 ResultVO 返回 |
cc.cc4414.spring.common.result.ResultVO<String> |
handlerException(javax.validation.ConstraintViolationException e)
参数异常(url上参数校验异常)
|
cc.cc4414.spring.common.result.ResultVO<String> |
handlerException(org.springframework.dao.DuplicateKeyException e)
主键或唯一键约束异常
数据库来处理唯一约束会更加准确 有唯一约束的时候尽量交给数据库来处理 |
cc.cc4414.spring.common.result.ResultVO<String> |
handlerException(Exception e)
未预期的异常
如果抛出了该异常,则会发送通知 |
cc.cc4414.spring.common.result.ResultVO<String> |
handlerException(org.springframework.web.bind.MethodArgumentNotValidException e)
参数异常(请求体中参数校验异常)
|
cc.cc4414.spring.common.result.ResultVO<String> |
handlerException(org.springframework.web.client.RestClientException e)
接口调用异常
http请求调用别人的时候发生的异常 如连接超时、404、500等 |
cc.cc4414.spring.common.result.ResultVO<Object> |
handlerException(cc.cc4414.spring.common.result.ResultException e)
业务异常c 代码中遇到非正常情况的返回,不要自己手动组装 ResultVO
直接抛出 ResultException 即可 该方法会帮助你组装返回结果的 |
void |
handlerHttpException(Exception e)
http请求异常
对于http请求异常,不做处理,交给spring处理 spring可以将http状态码更好的返回 |
boolean |
supports(org.springframework.core.MethodParameter returnType,
Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType) |
@ExceptionHandler(value=java.lang.Exception.class) public cc.cc4414.spring.common.result.ResultVO<String> handlerException(Exception e)
e - Exception@ExceptionHandler(value={org.springframework.web.HttpRequestMethodNotSupportedException.class,org.springframework.web.HttpMediaTypeNotSupportedException.class,org.springframework.web.HttpMediaTypeNotAcceptableException.class,org.springframework.web.bind.MissingPathVariableException.class,org.springframework.web.bind.ServletRequestBindingException.class,org.springframework.beans.ConversionNotSupportedException.class,org.springframework.beans.TypeMismatchException.class,org.springframework.http.converter.HttpMessageNotReadableException.class,org.springframework.http.converter.HttpMessageNotWritableException.class,org.springframework.web.multipart.support.MissingServletRequestPartException.class,org.springframework.validation.BindException.class,org.springframework.web.servlet.NoHandlerFoundException.class,org.springframework.web.context.request.async.AsyncRequestTimeoutException.class})
public void handlerHttpException(Exception e)
throws Exception
e - ExceptionException - 抛出异常@ExceptionHandler(value=cc.cc4414.spring.common.result.ResultException.class) public cc.cc4414.spring.common.result.ResultVO<Object> handlerException(cc.cc4414.spring.common.result.ResultException e)
e - ResultException@ExceptionHandler(value=org.springframework.web.client.RestClientException.class) public cc.cc4414.spring.common.result.ResultVO<String> handlerException(org.springframework.web.client.RestClientException e)
e - RestClientException@ExceptionHandler(value=org.springframework.dao.DuplicateKeyException.class) public cc.cc4414.spring.common.result.ResultVO<String> handlerException(org.springframework.dao.DuplicateKeyException e)
e - DuplicateKeyException@ExceptionHandler(value=javax.validation.ConstraintViolationException.class) public cc.cc4414.spring.common.result.ResultVO<String> handlerException(javax.validation.ConstraintViolationException e)
e - ConstraintViolationException@ExceptionHandler(value=org.springframework.web.bind.MethodArgumentNotValidException.class) public cc.cc4414.spring.common.result.ResultVO<String> handlerException(org.springframework.web.bind.MethodArgumentNotValidException e)
e - MethodArgumentNotValidExceptionpublic boolean supports(org.springframework.core.MethodParameter returnType,
@NonNull
Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType)
supports in interface org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>public Object beforeBodyWrite(Object body, @NonNull org.springframework.core.MethodParameter returnType, @NonNull org.springframework.http.MediaType selectedContentType, @NonNull Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType, @NonNull org.springframework.http.server.ServerHttpRequest request, @NonNull org.springframework.http.server.ServerHttpResponse response)
beforeBodyWrite in interface org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>Copyright © 2021. All rights reserved.