JAVA全局异常处理,

package com.greewon.error;

import java.io.IOException;

import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.BadSqlGrammarException;
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.greewon.response.JsonResponse;
import com.greewon.response.PagedResultUtils;
import com.greewon.shirojwt.JwtException;
import com.greewon.shirojwt.UserNamePasswordException;

/**
 * @author TANGSHUAI
 * @date 2020年10月28日 下午9:48:39
 * 
 */
@ControllerAdvice
public class ErrorController {
     
	/**
	 * 空值异常
	 * 
	 * @param mes
	 * @return
	 */
	@ExceptionHandler(NullPointerException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> nullPointerException(NullPointerException mes) {
     
		return PagedResultUtils.jsonResponseVO(null, true, 500, false, mes.getMessage());
	}

	/**
	 * token异常
	 * 
	 * @return
	 */
	@ExceptionHandler(JwtException.class)
	@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
	public @ResponseBody JsonResponse<String> jwtException() {
     
		return PagedResultUtils.jsonResponseVO(null, true, 401, false, "登录失效,系统将重新登录!");
	}

	/**
	 * 运行时异常
	 * 
	 * @param mes
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> runtimeException(RuntimeException mes) {
     
		return PagedResultUtils.jsonResponseVO(null, true, 500, false, mes.getMessage());
	}

	/**
	 * 身份认证异常
	 * 
	 * @return
	 */
	@ExceptionHandler(UnknownAccountException.class)
	@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
	public @ResponseBody JsonResponse<String> unknownAccountException() {
     
		return PagedResultUtils.jsonResponseVO(null, false, 401, false, "登录失效,系统将重新登录!");
	}

	/**
	 * 登陆异常
	 * 
	 * @param mes
	 * @return
	 */
	@ExceptionHandler(UserNamePasswordException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> userNamePasswordException(UserNamePasswordException mes) {
     
		return PagedResultUtils.jsonResponseVO(null, false, 500, false, mes.getMessage());
	}

	/**
	 * 文件传输异常
	 * 
	 * @param mes
	 * @return
	 */
	@ExceptionHandler(IOException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> iOException(IOException mes) {
     
		return PagedResultUtils.jsonResponseVO(null, false, 500, false, "文件传输异常!");
	}

	/**
	 * 其他异常
	 * 
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> exception() {
     
		return PagedResultUtils.jsonResponseVO(null, true, 500, false, "出现异常!");
	}

	/**
	 * 处理sql执行异常
	 * 
	 * @return
	 */
	@ExceptionHandler(BadSqlGrammarException.class)
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public @ResponseBody JsonResponse<String> badSqlGrammarException() {
     
		return PagedResultUtils.jsonResponseVO(null, true, 500, false, "数据库语法错误!");
	}

}

你可能感兴趣的:(问题处理记录,java)