Result结果集封装
2022/7/21 6:25:54
本文主要是介绍Result结果集封装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Result 结果集封装
Controller
-
Result
package com.yang.controller; public class Result { private Object data; private Integer code; private String msg; public Result() { } public Result(Object data, Integer code) { this.data = data; this.code = code; } public Result(Object data, Integer code, String msg) { this.data = data; this.code = code; this.msg = msg; } 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 getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
-
Code
package com.yang.controller; public class Code { public static final Integer SAVE_OK = 20011; public static final Integer UPDATE_OK = 20011; public static final Integer DELETE_OK = 20011; public static final Integer GET_OK = 20011; public static final Integer SAVE_ERR = 20010; public static final Integer UPDATE_ERR = 20010; public static final Integer DELETE_ERR = 20010; public static final Integer GET_ERR = 20010; public static final Integer SYSTEM_UNKNOWN_ERROR = 50001; public static final Integer SYSTEM_TIMEOUT_ERROR = 50002; public static final Integer PROJECT_VALIDATE_ERROR = 60001; public static final Integer PROJECT_BUSINESS_ERROR = 60002; }
-
BookController
package com.yang.controller; import com.yang.domain.Book; import com.yang.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/books") public class BookController { @Autowired private BookService bookService; @PostMapping public Result save(@RequestBody Book book) { boolean flag = bookService.save(book); return new Result(flag, flag ? Code.SAVE_OK:Code.SAVE_ERR); } @PutMapping public Result update(@RequestBody Book book) { boolean flag = bookService.update(book); return new Result(flag, flag ? Code.UPDATE_OK:Code.UPDATE_ERR); } @DeleteMapping("/{id}") public Result delete(@PathVariable Integer id) { boolean flag = bookService.delete(id); return new Result(flag, flag ? Code.DELETE_OK:Code.DELETE_ERR); } @GetMapping("/{id}") public Result select(@PathVariable Integer id) { Book book = bookService.select(id); Integer code = book !=null ?Code.GET_OK:Code.GET_ERR; String msg = book !=null ? "" :"数据查询失败,请重试!"; return new Result(book,code,msg); } @GetMapping public Result selectAll() { List<Book> bookList = bookService.selectAll(); Integer code = bookList !=null ?Code.GET_OK:Code.GET_ERR; String msg = bookList !=null ? "" :"数据查询失败,请重试!"; return new Result(bookList,code,msg); } }
-
ProjectExceptionAdvice
package com.yang.controller; import com.yang.exception.BusinessException; import com.yang.exception.SystemException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class ProjectExceptionAdvice { @ExceptionHandler(BusinessException.class) public Result doBusinessException(BusinessException be ){ //发送对应的消息给用户 return new Result(null,be.getCode(),be.getMessage()); } @ExceptionHandler(SystemException.class) public Result doSystemException(SystemException se ){ //发送特定消息给用户 //发送特定消息给运维人员,提醒维护 //记录日志 return new Result(null,se.getCode(),se.getMessage()); } @ExceptionHandler(Exception.class) public Result doException(Exception ex ){ //发送特定消息给用户 //发送特定消息给编程人员,提醒维护 //记录日志 return new Result(null,Code.SYSTEM_UNKNOWN_ERROR,"系统繁忙,请联系管理员"); } }
这篇关于Result结果集封装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25JAVA语音识别项目项目实战入门教程
- 2024-11-25JAVA云原生项目实战入门教程
- 2024-11-25Java语音识别项目入门:新手必读指南
- 2024-11-25Java语音识别项目入门:轻松开始你的第一个语音识别项目
- 2024-11-25Java语音识别项目入门详解
- 2024-11-25Java语音识别项目教程:从零开始的详细指南
- 2024-11-25JAVA语音识别项目教程:初学者指南
- 2024-11-25Java语音识别项目教程:初学者指南
- 2024-11-25JAVA云原生入门:新手指南与基础教程
- 2024-11-25Java云原生入门:从零开始的全面指南