Java自定义异常在项目中的应用

在Java的一些项目中,在需要提供对外接口时,常常会有必要自定义响应一些code和message(例:0000:Success,500:Error),特别是在对接移动端项目中最为常见。为更加方便提供这些接口的程序员的开发,可以应用Java的自定义异常处理来实现。

现有一移动端应用,需要对接我们项目,其中有一个用户登录接口,其接口的请求和响应参数如下:

申请参数

 

字段名

描述

允许为空

备注

username

用户名

扫描二维码关注公众号,回复: 760032 查看本文章

N

 

password

密码

N

 

……

 

 

 

返回数据

 

字段名

描述

允许为空

备注

code

响应码

N

 

message

响应信息

N

 

username

用户名

N

 

……

 

 

 

制定一套响应信息库,创建code.properties来存储响应信息库:

code0000=0000

msg0000=成功

code500=500

msg500=失败

code0001=0001

msg0001=用户名不能为空

code0002=0002

msg0002=密码不能为空

code0003=0003

msg0003=用户名或密码不正确

在这里,约定前缀是code和msg,后面带的内容(例0000)一定要与其对应的code值相同。

 

接下来编写自定义异常处理:

先创建一个响应信息对象ResponseData.java

 

public class ResponseData {
        public static final String CODE = "code";
        public static final String MSG = "msg";
	private String code;
	private String msg;
	
	public ResponseData() {}
	public ResponseData(String code) {
		this.setCode(code);
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = CODE+code;
		this.msg = PropertiesUtil.get("code", MSG+code);
	}
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

}

读取资源文件的工具类

public class PropertiesUtil {
	private static Properties prop = new Properties();
	
	public static String get(String fileName, String key){
		try {
			prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName+".properties"));
		} catch (IOException e) {
			new RuntimeException("不存在此("+fileName+")资源文件!");
		}
		return prop.getProperty(key);
	}
	
}

 

最后创建一个自定义异常类MyException.java

public class MyException extends RuntimeException {
	private String name;
	private ResponseData data;
	
	public MyException(ResponseData data) {
		super(data.getMsg());
		this.data = data;
	}
	public MyException(String name, ResponseData data) {
		super(name+":"+data.getMsg());
		this.name = name;
		this.data = data;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ResponseData getData() {
		return data;
	}
	public void setData(ResponseData data) {
		this.data = data;
	}

}

 

至此,自定义异常处理相关代码就编写完成。

接下看看它的应用:

public void testException() {
	try {
		String username = "admin",password = "111111";//充当请求参数
		//1、验证请求参数
		checkParams(username,password);
		//2、验证通过,响应user信息
		responseClient(username);
	} catch (MyException e) {
		responseClient(e.getData());
	} catch (Exception e) {
		responseClient(new ResponseData("500"));
	}
}

private void responseClient(String username) {
	responseClient(new ResponseData("0000"));
	System.out.println(username);
}

private void responseClient(ResponseData data) {
	System.out.println(data.getCode()+":"+data.getMsg());
}

private void checkParams(String username, String password) {
	if(StringUtils.isEmpty(username))
		throw new MyException(new ResponseData("0001"));
	if(StringUtils.isEmpty(password))
		throw new MyException(new ResponseData("0002"));
	if(!USERNMAE.equals(username) || !PASSWORD.equals(password))
		throw new MyException(new ResponseData("0003"));
}

 

这样做的好处就是可以将代码分块,当需要提供更复杂的接口时,可以更加清晰地写下处理流程;而且你会发现做一些参数或流程验证的时候,不再需要if...else...了,代码简单优美。

猜你喜欢

转载自javahuhui.iteye.com/blog/2245826