目录
表现层目录结构
1.数据封装
1.1自定义Result类
public class Result {
private Integer code;
private Object data;
private String message;
public Result() {
}
public Result(Integer code, Object data, String massage) {
this.data = data;
this.code = code;
this.message = massage;
}
public Result(Integer code, Object data) {
this.data = data;
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String massage) {
this.message = massage;
}
}
1.2 自定义状态码
public class Code {
public static final Integer ADD_OK = 20011;
public static final Integer DELETE_0K = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer ADD_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20048;
}
1.3控制器类
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public Result add(@RequestBody Book book) {
boolean flag = bookService.add(book);
return new Result(flag ? Code.ADD_OK : Code.ADD_ERR, flag);
}
@PutMapping
public Result update(@RequestBody Book book) {
boolean flag = bookService.update(book);
return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
boolean flag = bookService.delete(id);
return new Result(flag ? Code.DELETE_0K : Code.DELETE_ERR, flag);
}
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String massage = book != null ? "" : "数据查询失败,请重试";
return new Result(code, book, massage);
}
@GetMapping
public Result getAll() {
List<Book> list = bookService.getAll();
Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
String massage = list != null ? "" : "数据查询失败,请重试";
return new Result(code, list, massage);
}
}
2.异常处理器
所有的异常均抛出到表现层进行处理!!!
2.1自定义异常(系统级、业务级)
public class SystemException extends RuntimeException {
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = 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 class BusinessException extends RuntimeException {
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = 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;
}
}
2.2自定义异常编码(持续补充)
public class Code {
public static final Integer SYSTEM_ERR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
public static final Integer SYSTEM_UNKNOWN_ERR = 59999;
public static final Integer BUSINESS_ERR = 60002;
}
2.3 触发自定义异常
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean add(Book book) {
try {
int add = bookDao.add(book);
return add > 0;
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
}
}
@Override
public boolean update(Book book) {
try {
int update = bookDao.update(book);
return update > 0;
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
}
}
@Override
public boolean delete(Integer id) {
if (id <= 0) {
throw new BusinessException(Code.BUSINESS_ERR, "无效的id");
}
try {
int delete = bookDao.delete(id);
return delete > 0;
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
}
}
@Override
public Book getById(Integer id) {
if (id <= 0) {
throw new BusinessException(Code.BUSINESS_ERR, "无效的id");
}
try {
return bookDao.getById(id);
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
}
}
@Override
public List<Book> getAll() {
try {
return bookDao.getAll();
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器异常", e);
}
}
}
2.4拦截并处理异常
@RestControllerAdvice
public class ProjectExceptionAdvice {
//系统异常处理
@ExceptionHandler(SystemException.class)//定义处理哪一种异常
public Result doSystemException(SystemException ex) {
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(ex.getCode(), null, ex.getMessage());
}
//业务异常处理
@ExceptionHandler(BusinessException.class)//定义处理哪一种异常
public Result doBusinessException(BusinessException ex) {
return new Result(ex.getCode(), null, ex.getMessage());
}
//其他异常处理
@ExceptionHandler(Exception.class)//定义处理哪一种异常
public Result doException(BusinessException ex) {
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(Code.SYSTEM_UNKNOWN_ERR, null, "系统繁忙,请稍后再试");
}
}