Unified treatment of abnormal Controller layer and return

Unified treatment of abnormal Controller layer and return (1)
abnormal unified treatment Controller layer and return (2)
abnormal unified treatment Controller layer and return
First, why do it?
You usually do not know in writing Controller layer interface when ever noticed how throw an exception this process, if the first reaction was thinking about using a try-catch catch exceptions?
Unusual, because you do not pass the try-catch on the compilation checking, so you can take the initiative to capture abnormal but this deal is only suitable for the kind of compiler automatically remind the inspection and processing. However, if there is a runtime exception and you have not had time to think it's time to deal with what will happen? We can look at this is not unusual following runtime processing example:
@RestController
public class ExceptionRest {
@GetMapping ( "getNullPointerException")
public the Map <String, Object> getNullPointerException () {
the throw new new NullPointerException ( "there was a null pointer exception ");
}
}

In the above code based on the maven SpringMVC project using tomcat after launch, the browser initiates the following request:
HTTP: // localhost: 8080 / zxtest / getNullPointerException
results after the visit is this:

Browser received an error message

You can see, we throw a null pointer exception, and not caught in the Controller interface layer, resulting exception stack will return to the front-end browser, the user created a very bad experience.
In addition, the front end of the error message can be seen from the server and back-end systems using middleware type, the frame information and the type of information used, even if the back-end SQL exception is thrown, you can also see SQL exception parameter information specific inquiry, which is a moderate-risk security vulnerabilities, must be repaired.
PS: If the item above, then use SpringBoot may not be given the information at the front, because SpringBoot automatic error returned content to do a deal, we need to create a project that contains only SpringMVC to more complex scenarios now use Maven web template.
Second, how do unified treatment?
When this occurs when abnormal running time, the easiest way we think perhaps may throw an exception to the code with exception handling, as follows:
@RestController
public class ExceptionRest {
Private Logger log = LoggerFactory.getLogger ( ExceptionRest.class);
@GetMapping ( "getNullPointerException")
public the Map <String, Object> getNullPointerException () {
the Map <String, Object> = new new returnMap is the HashMap <String, Object> ();
the try {
the throw a NullPointerException new new ( "appears null pointer exception ");
the catch} (a NullPointerException E) {
log.error ( "null pointer exception occurred", E);
returnMap.put ( "Success", to false);
returnMap.put ( "mesg", "exception request. Please try again test ");
}
return returnMap is;
}
}

Because we manually place thrown plus handling and proper return occurred the return to the front of the abnormal content, therefore, it is to get the following when we launch again the same request in the browser:
{
Success: false,
mesg: "abnormal request, please try again later"
}

Looks like the problem has been resolved, but you can make sure you can have just captured an exception in all places exception may occur and deal with it? You can ensure that the team of others to do so?
Obviously, you need a unified exception caught with the treatment program.
2.1 Use the HandlerExceptionResolver
the HandlerExceptionResolver is an exception handler interface, will be able to capture all the configuration exceptions, we are here to achieve a unified Web-based exception handling and return results thrown Controller layer after its kind registered in the spring configuration file.
2.1.1 Basic use

Method 1: Use the HttpServletResponse return JSON information

@Controller
public class WebExceptionResolver implements HandlerExceptionResolver {
private Logger log = LoggerFactory.getLogger(WebExceptionResolver.class);
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
log.error(“请求{}发生异常!”,httpServletRequest.getRequestURI());
ModelAndView mv = new ModelAndView();
httpServletResponse.setContentType(“application/json;charset=UTF-8”);
httpServletResponse.setCharacterEncoding(“UTF-8”);
String retStr = “{“success”:false,“msg”:“请求异常,请稍后再试”}”;
try{
httpServletResponse.getWriter().write(retStr);
}catch(Exception ex){
log.error ( "WebExceptionResolver Exception Processing", EX);
}
// need to return empty to prevent aberrant ModelAndView other processors continue to be captured
return Music Videos;
}
}

Through the above process, all exceptions can be WebExceptionResolver Web tier captured and processed in resolveException then be used to unify HttpServletResponse return information and wish to return. The following is a request to return the same link to the browser's content:

Message returned the interception Web abnormalities

Second way: Using ModelAndView return JSON information

@Controller
public class WebExceptionResolver implements HandlerExceptionResolver {
private Logger log = LoggerFactory.getLogger(WebExceptionResolver.class);
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
log.error(“请求{}发生异常!”,httpServletRequest.getRequestURI());
ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
mv.addObject(“success”,“false”);
mv.addObject(“mesg”,“请求异常,请稍后再试”);
return mv;
}
}

These two effects as identical, the only difference is that the second embodiment requires the use of multiple introduced jackson-databind package.

com.fasterxml.jackson.core
jackson-the DataBind

2.1.2 Specific exception handling
Sometimes we need to do some processing in the event of a specific abnormality, then you only need to determine the type of exception handling can be captured separately:
@Controller
public class WebExceptionResolver the implements the HandlerExceptionResolver {
Private Logger log = LoggerFactory.getLogger (WebExceptionResolver.class);
@Override
public ModelAndView the resolveException (the HttpServletRequest HttpServletRequest, HttpServletResponse the HttpServletResponse, Object O, exception E) {
log.error ( "{} exception request occurs!", HttpServletRequest.getRequestURI ());
ModelAndView = Music Videos new new ModelAndView (new new MappingJackson2JsonView ());
mv.addObject ( "Success", "to false");
IF (a NullPointerException the instanceof E) {
mv.addObject ( "mesg", "null pointer exception request occurs, please try again later") ;
} the else IF (a ClassCastException the instanceof E) {
mv.addObject ( "mesg", "type conversion exception request occurs, please try again later");
} the else {
mv.addObject ( "mesg", "exception request occurs, please try again later");
}
return Music Videos;
}
}

2.1.3 Exception processing chain
if more implementations HandlerExceptionResolver exception class is present, then they will form a processing chain, which declare this case the pretreatment, which after treatment, the more spring in statement front profile the first processing class is more exception handling, or even can block exception, not processed subsequent processor.
PS: If you plan to intercept an exception in the current exception handler, to prevent further thrown out by another processor, then directly in the last returns an empty ModelAndView object can be. If you do not intend to intercept this exception, continue to let other processor, then it can return null.
// need to return empty to prevent abnormal ModelAndView other processors continue to be captured
return mv;
// returns null will not intercept abnormal, other processors can continue to handle the exception
return null;

2.2 @ExceptionHandler
after Spring3.2, SpringMVC introduced ExceptionHandler approach, making for exception handling easier and more accurate, the only thing you need to do is create a new Controller, and then inside with two notes to complete capture and process Controller layer all exceptions.
2.2.1 Basic use
a new Controller follows:
@ControllerAdvice
public class ExceptionConfigController {
@ExceptionHandler
public ModelAndView from exceptionHandler (Exception E) {
ModelAndView Music Videos new new ModelAndView = (new new MappingJackson2JsonView ());
mv.addObject ( "Success", to false);
Music Videos .addObject ( "mesg", "an exception request occurs, please try again later");
return Music Videos;
}
}

We are above code, plus the @ControllerAdvice class notes, indicating that it is an enhanced version of the controller, which then creates a return ModelAndView object exceptionHandler method, which add @ExceptionHandler notes indicate that this is an exception processing method, and then write a method in which a specific exception handling logic and return parameters can be, so to complete all the work, really too convenient.
We return to the browser after initiating a call the following results:
{
Success: false,
mesg: "Request exception occurred, please try again later"
}

2.2 specific exception handling
with respect HandlerExceptionResolver, respectively, using @ExceptionHandler more flexible handling of various abnormalities compared. And, when the specified exception is thrown exception subclass, it still can be captured and processed.
We change the code at the controller layer follows:
@RestController
public class ExceptionController {

@GetMapping("getNullPointerException")
public Map<String, Object> getNullPointerException() {
    throw new NullPointerException("出现了空指针异常");
}

@GetMapping("getClassCastException")
public Map<String, Object> getClassCastException() {
    throw new ClassCastException("出现了类型转换异常");
}

@GetMapping("getIOException")
public Map<String, Object> getIOException() throws IOException {
    throw new IOException("出现了IO异常");
}

}

ClassCastException NullPointerException known and inherited RuntimeException, and RuntimeException and IOException inherit Exception.
We do this in a process ExceptionConfigController:
@ControllerAdvice
public class ExceptionConfigController {
// designed to capture and process NullPointerException Controller layer
@ExceptionHandler (NullPointerException.class)
public ModelAndView nullPointerExceptionHandler (a NullPointerException E) {
ModelAndView Music Videos new new ModelAndView = (new new MappingJackson2JsonView ());
mv.addObject ( "Success", to false);
mv.addObject ( "mesg", "null pointer exception request occurs, please try again later");
return Music Videos;
}

// 专门用来捕获和处理Controller层的运行时异常
@ExceptionHandler(RuntimeException.class)
public ModelAndView runtimeExceptionHandler(RuntimeException e){
    ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
    mv.addObject("success",false);
    mv.addObject("mesg","请求发生了运行时异常,请稍后再试");
    return mv;
}

// 专门用来捕获和处理Controller层的异常
@ExceptionHandler(Exception.class)
public ModelAndView exceptionHandler(Exception e){
    ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
    mv.addObject("success",false);
    mv.addObject("mesg","请求发生了异常,请稍后再试");
    return mv;
}

}

Then

When we throw NullPointerException in the Controller layer, it will be processed nullPointerExceptionHandler, then interception.
{
Success: to false,
mesg: "a null pointer exception request occurs, please try again later"
}

When we throw a ClassCastException in the Controller layer, it will be processed runtimeExceptionHandler, then interception.
{
Success: to false,
mesg: "exception request occurs, please try again later running"
}

When we throw IOException the Controller layer, will be processed exceptionHandler, then interception.
{
Success: to false,
mesg: "exception request occurs, please try again later"
}

Third, the summary
Controller layer SpringMVC provides us with exception handling is really too convenient, especially @ExceptionHandler, we recommend using.

Author: force sheep
link: https://www.jianshu.com/p/d3fd250db622
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin blog.csdn.net/wcc178399/article/details/91143165