@ControllerAdvice@ExceptionHandler全局处理Controller层异常


@ControllerAdvice
public class BaseController {

    @ExceptionHandler()
    @ResponseBody
    String handleException(Exception e){
        return "Exception:" + e.getMessage();
    }
}
public class BusinessException extends RuntimeException {

    public BusinessException(String message) {
        super(message);
    }
}


@RestController
@RequestMapping(value = "/ex")
@Api(tags = "ExceptionTestAPI", description = "API", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CustomerExceptionHandlerController {

    @ApiOperation(value = "获用户详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/getuser/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable int id) {
        throw new BusinessException("该ID不存在");
    }
}




猜你喜欢

转载自blog.csdn.net/jonwu0102/article/details/79992685
今日推荐