- @RestController
- /*springboot提供了默认的错误映射地址error
- @RequestMapping("${server.error.path:${error.path:/error}}")
- @RequestMapping("/error")
- 上面2种写法都可以
- */
- @RequestMapping("/error")
- //继承springboot提供的ErrorController
- public class TestErrorController implements ErrorController {
- //一定要重写方法,默认返回null就可以,不然报错,因为getErrorPath为空.
- @Override
- public String getErrorPath() {
- return null;
- }
- //一定要添加url映射,指向error
- /* @RequestMapping()
- public Map<String, Object> handleError() {
- //用Map容器返回信息
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("code", 404);
- map.put("msg", "不存在");
- return map;
- }*/
- /*这里加一个能正常访问的页面,作为比较
- 因为写在一个控制器所以它的访问路径是
- http://localhost:8080/error/ok*/
- @RequestMapping("/ok")
- @ResponseBody
- public Map<String, Object> noError() {
- //用Map容器返回信息
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("code ", 200);
- map.put("msg", "正常,这是测试页面");
- return map;
- }
- //这里不要加consumes="text/html;charset=UTF-8",不然不成功,有部分浏览器提交的是空值
- @RequestMapping( value = "",produces = "text/html;charset=UTF-8")
- @ResponseBody
- public String errorHtml4040(HttpServletRequest request, HttpServletResponse response) {
- //跳转到error 目录下的 404模板
- return "404错误,不存在";
- }
- @RequestMapping(value = "", consumes="application/json;charset=UTF-8",produces = "application/json;charset=UTF-8")
- @ResponseBody
- public Map<String, Object> errorJson() {
- //用Map容器返回信息
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 404);
- map.put("rspMsg", "不存在");
- return map;
- }
- }
复制代码- @RestControllerAdvice
- public class GlobalExceptionHandler {
- // 日志记录工具
- private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(MissingServletRequestParameterException.class)
- public Map<String, Object> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
- logger.error("缺少请求参数", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 400);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(HttpMessageNotReadableException.class)
- public Map<String, Object> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
- logger.error("参数解析失败", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 400);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public Map<String, Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
- logger.error("参数验证失败", e);
- BindingResult result = e.getBindingResult();
- FieldError error = result.getFieldError();
- String field = error.getField();
- String code = error.getDefaultMessage();
- String message = String.format("%s:%s", field, code);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 400);
- map.put("rspMsg", message);
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(BindException.class)
- public Map<String, Object> handleBindException(BindException e) {
- logger.error("参数绑定失败", e);
- Map<String, Object> map = new HashMap<String, Object>();
- BindingResult result = e.getBindingResult();
- FieldError error = result.getFieldError();
- String field = error.getField();
- String code = error.getDefaultMessage();
- String message = String.format("%s:%s", field, code);
- map.put("rspCode", 400);
- map.put("rspMsg",message);
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(ConstraintViolationException.class)
- public Map<String, Object> handleServiceException(ConstraintViolationException e) {
- logger.error("参数验证失败", e);
- Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
- ConstraintViolation<?> violation = violations.iterator().next();
- String message = violation.getMessage();
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 400);
- map.put("rspMsg", message);
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(ValidationException.class)
- public Map<String, Object> handleValidationException(ValidationException e) {
- logger.error("参数验证失败", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 400);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 405 - Method Not Allowed
- */
- @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
- @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
- public Map<String, Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
- logger.error("不支持当前请求方法", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 405);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 415 - Unsupported Media Type
- */
- @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
- @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
- public Map<String, Object> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
- logger.error("不支持当前媒体类型", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 415);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 自定义异常类
- */
- @ResponseBody
- @ExceptionHandler(BusinessException.class)
- public Map<String, Object> businessExceptionHandler(BusinessException e) {
- logger.error("自定义业务失败", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", e.getCode());
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- /**
- * 获取其它异常。包括500
- *
- * @param e
- * @return
- * @throws Exception
- */
- @ExceptionHandler(value = Exception.class)
- public Map<String, Object> defaultErrorHandler(Exception e) {
- logger.error("Exception", e);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("rspCode", 500);
- map.put("rspMsg", e.getMessage());
- //发生异常进行日志记录,写入数据库或者其他处理,此处省略
- return map;
- }
- }
复制代码
Java其实迭代很快,从JDK 8以后发展的更快了
c#我不知道,.net core应该有吧
|
|