SSM学习记录5:前后端数据传输(/前后端传输协议) + 异常处理(注释方式 + SprigMVC项目 + 2022发布版本IDEA)
前后端数据传输(/前后端传输协议) + 异常处理
为与业界看齐,编写一个表示层的数据传输格式类,以下将格式规范化↓
Result类↓
public class Result {private Integer code;//保存对应操作成功或失败标记private String msg;//保存提示信息private Object data;//保存数据public Result() {}public Result(Integer code, Object data) {this.code = code;this.data = data;}public Result(Integer code, String msg, Object data) {this.code = code;this.msg = msg;this.data = data;}public void setCode(Integer code) {this.code = code;}public void setMsg(String msg) {this.msg = msg;}public void setData(Object data) {this.data = data;}public Integer getCode() {return code;}public String getMsg() {return msg;}public Object getData() {return data;}
}
Code类↓
public class Code {//与前端沟通好,确定各编码语义public static final Integer SAVE_OK = 20011;//保存成功public static final Integer DELETE_OK = 20021;//删除成功public static final Integer UPDATE_OK = 20031;//更新成功public static final Integer GET_OK = 20041;//查询成功public static final Integer SAVE_ERR = 20010;//保存失败public static final Integer DELETE_ERR = 20020;//删除失败public static final Integer UPDATE_ERR = 20030;//更新失败public static final Integer GET_ERR = 20040;//查询失败public static final Integer SYSTEM_ERR = 50001;//系统错误public static final Integer SYSTEM_TIMEOUT_ERR = 50002;//系统超时错误public static final Integer SYSTEM_UNKNOW_ERR = 59999;//系统未知异常public static final Integer BUSINESS_ERR = 60002;//业务错误
}
规范Controller类↓
@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/save")public String save(){System.out.println("你好");return "success";}@PostMappingpublic Result save(@RequestBody User user) {boolean flag = userService.save(user);return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);}@PutMappingpublic Result update(@RequestBody User user) {boolean flag = userService.update(user);return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);}@PutMapping("/{uAccount}")public Result updatePassword(@PathVariable String uAccount) {boolean flag = userService.updatePassword(uAccount);return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);}@DeleteMapping("/{uAccount}")public Result delete(@PathVariable String uAccount) {boolean flag = userService.delete(uAccount);return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);}@GetMapping("/{uAccount}")public Result getByAccount(@PathVariable String uAccount) {User user = userService.getByuAccount(uAccount);Integer code = user != null ? Code.GET_OK : Code.GET_ERR;String msg = user != null ? "查询成功" : "数据查询失败请重试";return new Result(code, msg, user);}@GetMappingpublic Result getAll() {List<User> userList = userService.getAll();Integer code = userList != null ? Code.GET_OK : Code.GET_ERR;String msg = userList != null ? "查询成功" : "数据查询失败请重试";return new Result(code, msg, userList);}}
异常
将异常拦截在表现层并抛出,编写一个拦截异常的类
ExceptionAdvice,使用@RestControllerAdvice注解表示Rest风格的表示层异常处理器类,@ExceptionHandler注解表示该方法处理异常类型
注:异常拦截后原始异常捕获不再执行,导致异常的操作不会有任何响应,异常处理将由注解的异常处理方法接管
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;@RestControllerAdvice
public class ExceptionAdvice {@ExceptionHandler(Exception.class)//接管对应类型(Exception)异常public Result dealException(Exception ex){System.out.println("出现异常:" + ex);ex.printStackTrace();return new Result(-1, "出现异常", "异常");}
}
项目异常细分,exception包
业务异常BusinessException↓
public class BusinessException extends RuntimeException {private Integer code;//编码,记录异常类型public BusinessException(Integer code, String message) {super(message);this.code = code;}public BusinessException(Integer code, String message, Throwable cause) {super(message, cause);this.code = code;}public void setCode(Integer code) {this.code = code;}public Integer getCode() {return code;}}
系统异常类SystemException↓
public class SystemException extends RuntimeException{private Integer code;//编码,记录异常类型public SystemException(Integer code, String message) {super(message);this.code = code;}public SystemException(Integer code, String message, Throwable cause) {super(message, cause);this.code = code;}public void setCode(Integer code) {this.code = code;}public Integer getCode() {return code;}
}
调整控制层异常处理器↓
抛出业务异常↓
web里的jsp文件中
使用<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
引入axios
使用<script src="https://unpkg.com/vue/dist/vue.js"></script>
引入vue
调取控制器方法↓
待更新