1. 程式人生 > >Spring Controller 全域性捕獲異常

Spring Controller 全域性捕獲異常

package controllerexception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ControllerException {

@ExceptionHandler(value = Exception.class)
public void exception(Exception e){
    e.printStackTrace();
}

}

package controllerexception;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(“/test”)
public class ControllerTest {

@RequestMapping("/value")
public void test(){
    throw new NullPointerException();
}

}