Search in sources :

Example 46 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project spring_boot by hryou0922.

the class BindExceptionHanlder method handleBindException.

@ExceptionHandler(BindException.class)
public Result handleBindException(BindException ex) {
    // ex.getFieldError():随机返回一个对象属性的异常信息。如果要一次性返回所有对象属性异常信息,则调用ex.getAllErrors()
    FieldError fieldError = ex.getFieldError();
    StringBuilder sb = new StringBuilder();
    sb.append(fieldError.getField()).append("=[").append(fieldError.getRejectedValue()).append("]").append(fieldError.getDefaultMessage());
    // 生成返回结果
    Result errorResult = new Result();
    errorResult.setCode(400);
    errorResult.setMessage(sb.toString());
    return errorResult;
}
Also used : FieldError(org.springframework.validation.FieldError) GlobalExceptionHandler(com.hry.spring.mvc.exceptionhandling.GlobalExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 47 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project spring_boot by hryou0922.

the class GlobalExceptionHandler method handleEmployeeNotFoundException.

@ExceptionHandler(EmployeeExJsonException.class)
@ResponseBody
public ExceptionJSONInfo handleEmployeeNotFoundException(HttpServletRequest request, Exception ex) {
    ExceptionJSONInfo response = new ExceptionJSONInfo();
    response.setUrl(request.getRequestURL().toString());
    response.setMessage(ex.getMessage());
    return response;
}
Also used : ExceptionJSONInfo(com.hry.spring.mvc.exceptionhandling.support.ExceptionJSONInfo) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 48 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.

the class BaseController method exception.

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ SignatureException.class, MalformedJwtException.class, JwtException.class })
public JwtResponse exception(Exception e) {
    JwtResponse response = new JwtResponse();
    response.setStatus(JwtResponse.Status.ERROR);
    response.setMessage(e.getMessage());
    response.setExceptionType(e.getClass().getName());
    return response;
}
Also used : JwtResponse(io.jsonwebtoken.jjwtfun.model.JwtResponse) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 49 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.

the class RestResponseEntityExceptionHandler method handleAccessDeniedException.

@ExceptionHandler({ RepositoryConstraintViolationException.class })
public ResponseEntity<Object> handleAccessDeniedException(Exception ex, WebRequest request) {
    RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex;
    String errors = nevEx.getErrors().getAllErrors().stream().map(ObjectError::toString).collect(Collectors.joining("\n"));
    return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RepositoryConstraintViolationException(org.springframework.data.rest.core.RepositoryConstraintViolationException) ResponseEntity(org.springframework.http.ResponseEntity) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 50 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.

the class FileUploadExceptionAdvice method handleMaxSizeException.

@ExceptionHandler(MaxUploadSizeExceededException.class)
public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response) {
    ModelAndView modelAndView = new ModelAndView("file");
    modelAndView.getModel().put("message", "File too large!");
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)178 ResponseEntity (org.springframework.http.ResponseEntity)58 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)48 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)41 HttpHeaders (org.springframework.http.HttpHeaders)39 ModelAndView (org.springframework.web.servlet.ModelAndView)33 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)31 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)17 HttpStatus (org.springframework.http.HttpStatus)13 AjaxJson (com.cdeledu.common.base.AjaxJson)12 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)8 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)8 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)8 RestResponse (org.entando.entando.web.common.model.RestResponse)8 ArrayList (java.util.ArrayList)7 BindingResult (org.springframework.validation.BindingResult)6 FieldError (org.springframework.validation.FieldError)5 ErrorInfo (com.haulmont.restapi.exception.ErrorInfo)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4