70218793.rar

  • HYUewqdw@00197
    了解作者
  • Java
    开发工具
  • 315KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2018-02-09 09:17
    上传日期
spring boot+JPA+easyUI 实现基于浏览器语言的国际化配置
70218793.rar
内容介绍
package com.jege.spring.boot.exception; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.ValidationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import com.jege.spring.boot.json.AjaxResult; @ControllerAdvice @ResponseBody public class CommonExceptionAdvice { private static Logger logger = LoggerFactory.getLogger(CommonExceptionAdvice.class); /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingServletRequestParameterException.class) public AjaxResult handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { logger.error("缺少请求参数", e); return new AjaxResult().failure("required_parameter_is_not_present"); } /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) public AjaxResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { logger.error("参数解析失败", e); return new AjaxResult().failure("could_not_read_json"); } /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException.class) public AjaxResult 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); return new AjaxResult().failure(message); } /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(BindException.class) public AjaxResult handleBindException(BindException 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); return new AjaxResult().failure(message); } /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ConstraintViolationException.class) public AjaxResult handleServiceException(ConstraintViolationException e) { logger.error("参数验证失败", e); Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); ConstraintViolation<?> violation = violations.iterator().next(); String message = violation.getMessage(); return new AjaxResult().failure("parameter:" + message); } /** * 400 - Bad Request */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(ValidationException.class) public AjaxResult handleValidationException(ValidationException e) { logger.error("参数验证失败", e); return new AjaxResult().failure("validation_exception"); } /** * 405 - Method Not Allowed */ @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public AjaxResult handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { logger.error("不支持当前请求方法", e); return new AjaxResult().failure("request_method_not_supported"); } /** * 415 - Unsupported Media Type */ @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public AjaxResult handleHttpMediaTypeNotSupportedException(Exception e) { logger.error("不支持当前媒体类型", e); return new AjaxResult().failure("content_type_not_supported"); } /** * 500 - Internal Server Error */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(ServiceException.class) public AjaxResult handleServiceException(ServiceException e) { logger.error("业务逻辑异常", e); return new AjaxResult().failure("业务逻辑异常:" + e.getMessage()); } /** * 500 - Internal Server Error */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(Exception.class) public AjaxResult handleException(Exception e) { logger.error("通用异常", e); return new AjaxResult().failure("通用异常:" + e.getMessage()); } /** * 操作数据库出现异常:名称重复,外键关联 */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(DataIntegrityViolationException.class) public AjaxResult handleException(DataIntegrityViolationException e) { logger.error("操作数据库出现异常:", e); return new AjaxResult().failure("操作数据库出现异常:字段重复、有外键关联等"); } }
评论
    相关推荐
    • windows编程
      windows编程技术 Windows的DLL编程 Windows多线程技术 Windows应用程序的基本结构 window菜单编程 window绘图机制 WinSock编程 对话框编程
    • windows编程
      含简体和繁体两种 源代码见我的资源
    • WINDOWS编程必备
      包括常见的windows API的介绍和windows编程知识的介绍,是学习windows编程的入门及进阶资料哦
    • windows编程
      windows编程基本理论
    • Windows编程体系
      有关window编程的课件及源码,比较基础的windows编程讲解,适合windows编程的初学者,且里边的知识比较实用,且对windows程序运行讲解得较清楚
    • windows编程API手册
      详细讲述了windows API函数的功能与用法,全面细致!奉献出来与大家共同学习!
    • Windows编程圣经
      这是一个帮助文档,包含了Windows常用的各种各样的api,还有例子共大家参考。
    • Windows编程新手教程
      本教程主要包括了windows开发基础知识,以及注意点和技巧
    • Windows编程四大件
      Programing Windows By Charles Petzold Programming Applications for Microsoft Windows Programming Windows with MFC 2nd ed Programming with Microsoft Visual C++.NET(6th ed)
    • Windows编程PPT课件
      课件以C++为编程基础,从Windows基础知识到Windows内部开发环境,全面讲解,以实例说明,由浅入深,通俗易懂,如果您想了解Windows编程,很适合