我们采用RESTful API 时,为了让接口看起来更清晰,更优雅,我们需要对一些特殊情况进行一些处理。
有时由于,返回的数据不一样,造成返回json数据的格式不一样,看起来很别扭,例如:
DELETE http://localhost:8080/hero
//什么也没有返回POST http://localhost:8080/hero
{ "id": 6, "name": "刘备" }能不能,一眼就看出来执行有没有成功:例如POST接口我想返回类似这样的数据:
POST http://localhost:8080/hero
//状态码,提示信息,数据 { "code": 1, "msg": "成功", "data": { "id": 7, "name": "刘备" } }在这里我对返回成功或者失败进行了封装,封装成了ResultUtil 工具类
public class ResultUtil { public static Result success(Object object){ Result result = new Result(); result.setCode(1); result.setMsg("成功"); result.setData(object); return result; } public static Result success(){ return success(null); } public static Result error(Integer code , String msg){ Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }现在在对新增方法进行测试发现,已经实现了预期的效果,返回json数据格式如下:
传递name=”张飞”看看结果
发现我们设置的Result类不在起作用,返回的json数据又乱了。现在我们需要对Exception进行捕获处理。
在自定义异常时,一定要注意Spring 只回滚RuntimeException 异常,如果继承Exception spring是不回滚的
public class HeroException extends RuntimeException{ private Integer code; public HeroException(Integer code, String message){ super(message); this.code = code; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }对自己抛出的异常进行捕获处理,对系统异常进行提示,并写入日志。
@ControllerAdvice public class ExceptionHandle { private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ if(e instanceof HeroException){ HeroException heroException = (HeroException) e; return ResultUtil.error(heroException.getCode(), heroException.getMessage()); } else { logger.error("【系统异常】{}",e); return ResultUtil.error(-1, "未知错误"); } } }现在的效果就是我们所预期的了
参考资料:http://www.imooc.com/video/14341