After the SSM integration and functional module development are completed, next, we will analyze what problems we need to solve based on the above cases. First of all, the first question is:
The boolean type data returned to the front end when adding, deleting or modifying at the Controller layer
Querying a single data returned to the front end at the Controller layer It is an object
Query at the Controller layer and all returned to the front end are collection objects
Currently we have three types The data type is returned to the front end. As the business grows, we will need to return more and more data types. For front-end developers, it is quite messy when parsing data, so for the front-end, if the background can return a unified data result, the front-end can parse in one way when parsing. Development will become easier.
So we wonder if we can unify the returned result data and how to do it. The general idea is:
In order to encapsulate the returned result data: create Result model class, encapsulate data into the data attribute
In order to encapsulate what kind of operation the returned data is and whether the operation is successful: encapsulate the operation result into the code attribute
In order to encapsulate the error message returned after the operation fails: encapsulate the special message into the message (msg) attribute
According to the analysis, we You can set up a unified data return result class
public class Result{ private Object data; private Integer code; private String msg; }
Note: The Result class name and the fields in the class are not fixed. You can increase or decrease it as needed to provide several construction methods for convenient operation.
For result encapsulation, we should process it in the presentation layer, so we put the result class in the controller package Of course, you can also put it in the domain package. This is possible. How to implement result encapsulation? The specific steps are:
Step 1: Create the Result class
public class Result { //描述统一格式中的数据 private Object data; //描述统一格式中的编码,用于区分操作,可以简化配置0或1表示成功失败 private Integer code; //描述统一格式中的消息,可选属性 private String msg; public Result() { } //构造方法是方便对象的创建 public Result(Integer code,Object data) { this.data = data; this.code = code; } //构造方法是方便对象的创建 public Result(Integer code, Object data, 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; } @Override public String toString() { return "Result{" + "data=" + data + ", code=" + code + ", msg='" + msg + '\'' + '}'; } }
Note:
You don’t need to write the toString method, it will be converted to json format in the end. But getter and setter methods must be present!
Step 2: Define the return code Code class
//状态码 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; }
Note: The constant design in the code class is not fixed and can be increased or decreased according to needs, for example, the query is subdivided into GET_OK ,GET_ALL_OK,GET_PAGE_OK, etc.
Step 3: Modify the return value of the Controller class
//统一每一个控制器方法返回值 @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 ? Code.SAVE_OK:Code.SAVE_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_OK: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 msg = book != null ? "" : "数据查询失败,请重试!"; return new Result(code,book,msg); } @GetMapping public Result getAll() { List<Book> bookList = bookService.getAll(); Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR; String msg = bookList != null ? "" : "数据查询失败,请重试!"; return new Result(code,bookList,msg); } }
The bookList is compared with null here because if no results are found in all queries, a null will be returned directly instead of returning An empty list!
When checking the book based on the ID, if it is not found, it will return a null
Finally our project structure:
Steps 4: Start the service test
#Our return results can already be returned to the front end in a unified format. Based on the returned result, the front end first obtains code
from it. Based on the code judgment, if it succeeds, it takes the value of the data
attribute. If it fails, it takes the value of msg
. Value as a reminder.
The above is the detailed content of Java SSM integrated development unified result encapsulation example analysis. For more information, please follow other related articles on the PHP Chinese website!