[첫 편]springboot 이상 트 랜 잭 션 로그 처리
30531 단어 자바
Java framework for enterprise web applications
[첫 번 째 이상 한 통 일 된 처리]
이상 을 통일 적 으로 처리 하 였 으 며,@Aspect 와@RestController Advice 를 사용 하여 통일 적 으로 처리 하 였 습 니 다.
@RestControllerAdvice
@Slf4j
public class TalExceptionHandler {
/**
* json
* @param cve
* @param httpServletRequest
* @return
*/
@ExceptionHandler(ConstraintViolationException.class)
public Response methodArgNotValidException(ConstraintViolationException cve,
HttpServletRequest httpServletRequest) {
log.error(cve.getMessage(), cve);
Set<ConstraintViolation<?>> cves = cve.getConstraintViolations();
StringBuffer errorMsg = new StringBuffer();
cves.forEach(ex -> errorMsg.append(ex.getMessage()));
Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), errorMsg.toString());
return response;
}
/**
* param
* @param ex
* @param request
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class})
public Response methodDtoNotValidException(Exception ex, HttpServletRequest request) {
log.error(ex.getMessage(), ex);
MethodArgumentNotValidException c = (MethodArgumentNotValidException) ex;
List<ObjectError> errors = c.getBindingResult().getAllErrors();
StringBuffer errorMsg = new StringBuffer();
errors.stream().forEach(x -> {
errorMsg.append(x.getDefaultMessage()).append(";");
});
Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), errorMsg.toString());
return response;
}
/**
* 400 - Bad Request
*/
@ExceptionHandler({MissingServletRequestParameterException.class})
public Response handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.error(" ", e);
Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()),
" " + e.getParameterName());
return response;
}
/**
*
*
* @param e
* @param req
* @return
*/
@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class})
public Response httpMessageNotReadableException(Exception e, WebRequest req) {
log.error(e.getMessage(), e);
Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), " ");
return response;
}
/**
* Service
*
* @param e
* @return
* @throws Exception
*/
@ExceptionHandler(ParamException.class)
public Response paramException(ParamException e) {
return new Response(e.getCode(), e.getMessage());
}
/**
* 。 500
*
* @param e
* @return
* @throws Exception
*/
@ExceptionHandler({Exception.class})
public Response defaultErrorHandler(Exception e) {
log.error(" Exception", e);
Response response = new Response(TkResponseCode.ERROR_NORMAL, " ");
return response;
}
}
/**
*
*
*
* @author
*/
public class Response<T> implements Serializable
{
private static final long serialVersionUID = 6766428592214605847L;
/**
*
*/
@ApiModelProperty(value = " ")
private T responseEntity;
/**
*
*/
@ApiModelProperty(value = " ")
private PageInfo<T> pageInfo;
/**
*
*/
@ApiModelProperty(value = " ")
private MessageResult serverResult;
public Response()
{
this.serverResult = getServerResult();
}
public Response(T t)
{
this.responseEntity = t;
this.serverResult = getServerResult();
}
public Response(List<T> list)
{
if (list instanceof Page)
{
this.pageInfo = new PageInfo<T>(list,((Page<T>) list).getPages());
}
else
{
this.pageInfo = new PageInfo<T>();
this.pageInfo.setList(list);
this.pageInfo.setTotal(null == list ? 0 : list.size());
}
this.serverResult = getServerResult();
}
public Response(String resultCode,String resultMessage,String internalMessage)
{
this.serverResult = new MessageResult();
serverResult.setResultCode(resultCode);
serverResult.setResultMsg(resultMessage);
serverResult.setInternalMsg(internalMessage);
}
public Response(String resultCode,String resultMessage)
{
this(resultCode,resultMessage,resultMessage);
}
public Response(String resultCode)
{
this(resultCode, TkResponseGetEContent.getEContent(resultCode), TkResponseGetEContent.getEContent(resultCode));
}
public MessageResult getServerResult()
{
/**
*
*/
if (serverResult == null)
{
serverResult = new MessageResult();
serverResult.setResultCode(IStateCode.HTTP_200);
serverResult.setResultMsg(" ");
}
return serverResult;
}
public void setServerResult(MessageResult serverResult)
{
this.serverResult = serverResult;
}
public T getResponseEntity()
{
return responseEntity;
}
public void setResponseEntity(T responseEntity)
{
this.responseEntity = responseEntity;
}
public PageInfo<T> getPageInfo()
{
return pageInfo;
}
public void setPageInfo(PageInfo<T> pageInfo)
{
this.pageInfo = pageInfo;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.