//@ControllerAdvice+@ResponseBody,开启全局的异常捕获,返回JSON
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理自定义异常
* @return Result
* @ExceptionHandler 说明捕获哪些异常,对那些异常进行处理。
*/
@ExceptionHandler(value = DefinitionException.class)
public Result customExceptionHandler(DefinitionException e) {
return Result.defineError(e);
}
/**
* 处理其他异常
* @return Result
*/
@ExceptionHandler(value = Exception.class)
public Result exceptionHandler(Exception e) {
return Result.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}
说明:将对象解析成json,是为了方便前后端的交互。
三、代码测试与结果
测试类
ResultController:测试的controller类
@RestController
public class ResultController {
//获取学生信息
@GetMapping("/student")
public Result getStudent() {
Student student = new Student();
student.setId(1);
student.setAge(18);
student.setName("XuWwei")
return new Result<>(ResultEnum.SUCCESS, student);
}
//自定义异常处理
@RequestMapping("/getDeException")
public Result DeException() {
throw new DefinitionException(400, "我出错了");
}
//其他异常处理
@RequestMapping("/getException")
public Result Exception(){
Result result = new Result();
int a=1/0;
return result;
}
1、java.lang.IllegalStateException: Class invariant violation
at org.apache.log4j.LogManager.getLoggerRepository(LogManager.java:199)at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
at o
hadoop jps出现process information unavailable提示解决办法
jps时出现如下信息:
3019 -- process information unavailable3053 -- process information unavailable2985 -- process information unavailable2917 --
<?php
class Image{
private $path;
function __construct($path='./'){
$this->path=rtrim($path,'/').'/';
}
//水印函数,参数:背景图,水印图,位置,前缀,TMD透明度
public function water($b,$l,$pos