import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException e) {
String message = e.getMessage();
return new Result(20010,message);
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(Exception e) {
String message = e.getMessage();
return new Result(20010,message);
}
@ExceptionHandler(Exception.class)
public Result doexception(Exception e) {
System.out.println("走了exception异常");
String message = e.getMessage();
return new Result(20010,message);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc824e2731f2a51cc07da8692741fb9f.png)
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan("com.itcast.controller")
@EnableWebMvc
public class SpringMvcConfig {
}
public class BusinessException extends RuntimeException{}
public class SystemException extends RuntimeException{}