mirror of
				https://gitee.com/coder-xiaomo/flashsale
				synced 2025-11-04 22:23:14 +08:00 
			
		
		
		
	异常处理提取到BaseController
This commit is contained in:
		@@ -0,0 +1,34 @@
 | 
			
		||||
package com.cxyxiaomo.flashsale.controller;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.flashsale.error.BusinessException;
 | 
			
		||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
 | 
			
		||||
import com.cxyxiaomo.flashsale.response.CommonReturnType;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.web.bind.annotation.ExceptionHandler;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseBody;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseStatus;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
public class BaseController {
 | 
			
		||||
    // 定义ExceptionHandler解决未被Controller层吸收的Exception
 | 
			
		||||
    @ExceptionHandler(Exception.class)
 | 
			
		||||
    @ResponseStatus(HttpStatus.OK)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Object handlerException(HttpServletRequest request, Exception ex) {
 | 
			
		||||
 | 
			
		||||
        HashMap<Object, Object> responseData = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
        if (ex instanceof BusinessException) {
 | 
			
		||||
            BusinessException businessException = (BusinessException) ex;
 | 
			
		||||
            responseData.put("errCode", businessException.getErrCode());
 | 
			
		||||
            responseData.put("errMsg", businessException.getErrMsg());
 | 
			
		||||
        } else {
 | 
			
		||||
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
 | 
			
		||||
            responseData.put("errMsg", EmBusinessError.UNKNOWN_ERROR.getErrMsg());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return CommonReturnType.create(responseData, "fail");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,22 +2,18 @@ package com.cxyxiaomo.flashsale.controller;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.flashsale.controller.viewobject.UserVO;
 | 
			
		||||
import com.cxyxiaomo.flashsale.error.BusinessException;
 | 
			
		||||
import com.cxyxiaomo.flashsale.error.EmBusinessError;
 | 
			
		||||
import com.cxyxiaomo.flashsale.response.CommonReturnType;
 | 
			
		||||
import com.cxyxiaomo.flashsale.service.UserService;
 | 
			
		||||
import com.cxyxiaomo.flashsale.service.model.UserModel;
 | 
			
		||||
import org.springframework.beans.BeanUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.stereotype.Controller;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
@Controller("user") // 允许被SpringBoot扫描到
 | 
			
		||||
@RequestMapping("/user") // 通过 "/user" 访问到
 | 
			
		||||
public class UserController {
 | 
			
		||||
public class UserController extends BaseController {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
@@ -50,24 +46,4 @@ public class UserController {
 | 
			
		||||
        BeanUtils.copyProperties(userModel, userVO);
 | 
			
		||||
        return userVO;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 定义ExceptionHandler解决未被Controller层吸收的Exception
 | 
			
		||||
    @ExceptionHandler(Exception.class)
 | 
			
		||||
    @ResponseStatus(HttpStatus.OK)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Object handlerException(HttpServletRequest request, Exception ex) {
 | 
			
		||||
 | 
			
		||||
        HashMap<Object, Object> responseData = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
        if (ex instanceof BusinessException) {
 | 
			
		||||
            BusinessException businessException = (BusinessException) ex;
 | 
			
		||||
            responseData.put("errCode", businessException.getErrCode());
 | 
			
		||||
            responseData.put("errMsg", businessException.getErrMsg());
 | 
			
		||||
        } else {
 | 
			
		||||
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
 | 
			
		||||
            responseData.put("errMsg", EmBusinessError.UNKNOWN_ERROR.getErrMsg());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return CommonReturnType.create(responseData, "fail");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user