SpringBoot global exception handler @RestControllerAdvice + @ ExceptionHandler verification request parameter

ControllerAdvice

Indicates a class annotated secondary "controller."

As the specialized , it allows automatic detection of the scanning path based implementation class.@Component

Often used to define and  applicable to all methods of.@ExceptionHandler@InitBinder@ModelAttribute@RequestMapping

One of the , or its alias  can be specified to define the controller to assist in the particular subset. When applying a plurality of selectors, will be applied "or" logic - indicates the controller should be selected to match at least one selector.annotations()basePackageClasses()basePackages()value()

The default behavior (that is, if you do not use any selector uses), with a @ControllerAdviceclass comment will help all known Controller.

Please note that these checks are done at runtime, so add a lot of property and use a variety of strategies may have a negative impact (complexity, performance).


/ **
* SpringMVC unified exception handling
* Note: @ControllerAdvice for the Controller layer enhancer, which can only handle exceptions thrown Controller layer;
* The call mechanism hierarchy between codes, exception handling mechanism, the processing Controller layer here abnormalities, equivalent
* global exception handling
* @author
* /

@RestControllerAdvice
public class GlobalExceptionHandler {


@ExceptionHandler (MethodArgumentNotValidException.class)
public Object BindException (MethodArgumentNotValidException E) {
the BindingResult BindingResult e.getBindingResult = ();
the StringBuilder new new SB = the StringBuilder ();
sb.append ( "base data checks by:");
for (FieldError fielderror: bindingResult.getFieldErrors ()) {
sb.append ( "\ n-");
sb.append(fieldError.getDefaultMessage());
}
Map<String,Object> result=new HashMap<>();
result.put("code","00000010");
result.put("data",sb.toString());
return result;
}

}


@Length (min = 12, message = " Order Number incorrect length") 
@NotBlank (Message = "Order Number is not null")
String the orderId;

Guess you like

Origin www.cnblogs.com/xiqoqu/p/12016770.html