Spring 統一異常處理

1.返回給頁面的json工具類java

public class JsonResultUtil {  
    	@SuppressWarnings({ "rawtypes", "unchecked" })
    	private static Map<String,Object> json = new HashMap(0);
    	
    	public static Map<String,Object> success(){
    		return success(0,"操做成功");
    	}
    	
    	public static Map<String,Object> success(Integer code,String message){
    		json.put("code", code);
    		json.put("msg", message);
    		return json; 
    	}
    	
    	public static Map<String,Object> error(Integer code,String message){
    		json.put("code", code);
    		json.put("msg", message);
    		return json; 
    	} 	
    }

2. 流程狀態枚舉類json

public enum ResultEnum {
    	
    	NORMAL(0,"正常"),
    	
    	FLOW_END_ERROR(804,"【操做失敗】流程已結束, 請聯繫系統管理員"),
    	;
    	
    	ResultEnum(Integer code,String message){
    		this.code = code;
    		this.message = message;
    	}
    	
    	private Integer code;
    	private String message;
    	
    	public Integer getCode() {
    		return code;
    	}
    
    	public String getMessage() {
    		return message;
    	}   	
    }

3.編寫統一異常處理類(Spring只有拋出RuntimeException這個異常類纔會執行事務的回滾)ide

public class MyException extends RuntimeException{
    
    private Integer code;
    
    public MyException(ResultEnum resultEnum){
    super(resultEnum.getMessage());
    this.code = resultEnum.getCode();
    }
    
    public MyException(Integer code,String message){
    super(message);
    this.code = code;
    }
    
    	public Integer getCode() {
    		return code;
    	}
    
    	public void setCode(Integer code) {
    		this.code = code;
    	}  
    }

4. 在Service層編寫業務(拋出異常後不執行後面的代碼)工具

@Service
    public class UserServiceImpl implements UserService{
    
    	@Autowired
    	private UserDao userDao;
    
    	@Override
    	@Transactional
    	public void ex(){
    		User user = new User();
    		user.setName("_Artisan");
    		userDao.save(user);
    		if(2>1){
    			throw new MyException(ResultEnum.FLOW_END_ERROR);
    		}
    		System.out.println("###### haha #######");
    	}  
    }

5. 在Controller捕獲異常(拋出異常後面的代碼仍然執行),返回給錯誤信息給頁面。頁面接收到信息可作對應的彈窗提示測試

@Controller
    public class UserJson implements ModelDriven<User>{
    
    	private User user;
    	@Autowired
    	private UserService userService;
    	private Map<String,Object> json = JsonResultUtil.success();
    
    	private void printJson(Object o) {
    		getPrintWriter().write(JSONObject.toJSONString(o));
    	}
    
    	// 測試
    	public void test(){
    		try{
    			userService.ex();
    		}catch(MyException e){
    			json = JsonResultUtil.error(e.getCode(), e.getMessage());
    		}
    		printJson(json);
    	}
    
    	public PrintWriter getPrintWriter() {
    	if(null == printWriter){
    		try {
    			printWriter = getResponse().getWriter();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    		return printWriter;
    	}
    
    	@Override
    	public User getModel() {
    		if (s == null) {
    			s = new User();
    		}
    		return s;
    	}
    
    	// 省略User get、set的方法
    
    }
相關文章
相關標籤/搜索