Springboot: exception处理

 

MVC:

package us.transcode.thymeleaf.exception;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver{

  /**
   * ModelAndView 模型(数据)和视图, resolveException 当controller任意一个方法出现异常, 该controller没有自己处理异常, 进入该方法
   *
   * @param request  当前请求对象
   * @param response 当前请求响应对象
   * @param handler  出现错误的方法对象
   * @param ex       异常对
   * @return ModelAndView
   */
  @Override
  public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
    System.out.println("Global HandlerExceptionResolver");
    System.out.println("Exception: " + ex.getMessage());
    ModelAndView modelAndView = new ModelAndView();
    if(ex instanceof UsernameNotFoundException){
      modelAndView.setViewName("login");
      return modelAndView;
    }
    modelAndView.setViewName("500");
    return modelAndView;
  }
}
package us.transcode.thymeleaf.exception;


public class UsernameNotFoundException extends RuntimeException{
  public UsernameNotFoundException(String message){
    super(message);
  }
}

 

RESTFUL:

ExceptionController:

package us.vellum.representational.controller;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import us.vellum.representational.exception.IllegalNumberException;

@RestController
@RequestMapping(value = {"exception"})
public class ExceptionController{
  @GetMapping
  public ResponseEntity<String> demo(){
    int n = 1 / 0;
    return new ResponseEntity<>("demo ok", HttpStatus.ALREADY_REPORTED);
  }

  @GetMapping(value = "/{id}")
  public ResponseEntity<String> id(@PathVariable Integer id){
    System.out.println("id = " + id);
    System.out.println("(id<0) = " + (id < 0));
    if(id < 0) throw new IllegalNumberException(String.format("%05d invalid", id));
    return new ResponseEntity<>(String.format("id: %08d", id), HttpStatus.GONE);
  }
}

 

异常处理类:

package us.vellum.representational.exception;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionResolver{
  @ExceptionHandler(value = Exception.class)  // 用在方法, 处理指定异常, value指定异常类型
  @ResponseBody
  public ResponseEntity<String> generalExceptionHandler(Exception ex){
    System.out.println("ExceptionHandler");
    if(ex instanceof RuntimeException){
      System.out.println("ex instanceof RuntimeException");
      return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(ex.getMessage(), HttpStatus.GATEWAY_TIMEOUT);
  }

  @ExceptionHandler(value = IllegalNumberException.class)
  @ResponseBody
  public ResponseEntity<String> illegalNumberExceptionHandler(Exception ex){
    System.out.println("IllegalNumberException");
    return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.CONFLICT);
  }
}

自定义异常类

package us.vellum.representational.exception;


public class IllegalNumberException extends RuntimeException{
  public IllegalNumberException(String message){
    super(message);
  }
}
package us.vellum.representational.exception;


public class UsernameNotFoundException extends RuntimeException{
  public UsernameNotFoundException(String message){
    super(message);
  }
}

 

posted @ 2022-04-28 21:44  ascertain  阅读(145)  评论(0编辑  收藏  举报