SSMP integration case (9) Unified presentation layer data return format

The above SSMP integration case (8) Restful development of the presentation layer interface We have already written the presentation layer interface
, but we will find that now the front-end personnel get our data format and it looks very messy.
Our database is just adding, modifying and deleting data. The format of the query is a Boolean value , the query
is to check multiple collections,
the query is all in the format of an object
, and the format of a single object in an IPage for pagination
is varied.

Different data analysis methods are different. Front-end colleagues will be very collapsed when they look at it

Then we can unify. For example, the data we return can be placed in the field of data,
but in this way, null will appear when we make a query error or fail to find it.

Then we will give the front end something like this

{
    
    
    data: null
}

This is very controversial because this null is not necessarily caused by no data, it may also be caused by system exceptions and some external factors

So only one data is obviously not enough. Our front-end colleagues obviously don’t know how to prompt the user whether the data is abnormal or what’s wrong.
Let’s add a state. If it is 200, it means success.

{
    
    
    data: {
    
    
        state: 200,
        name: 数据
    }
}

failure is

{
    
    
    data: {
    
    
        state: 500,
        name: null
    }
}

For details, you can go to understand the HTTP status code
, but various problems may also occur in the background when we are running.
We also need to give an output statement
message
, such as

{
    
    
    data: {
    
    
        state: 200,
        name: 数据,
        message: "操作成功"
    }
}

or

{
    
    
    data: {
    
    
        state: 500,
        name: null,
        message: "系统异常"
    }
}

Create a package called utils under the controller package before our project.
Create a ResultUtil class ResultUtil under the utils package.
insert image description here
The reference code is as follows

package com.example.webdom.controller.utils;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ResultUtil {
    
    
    private int state;
    private Object data;
    private String message;
}

I believe you still remember that lombok’s Data automatically declares the get and set methods.
Otherwise, you still have to write the get and set functions yourself.
Then we added a construction method with all parameters to it through AllArgsConstructor. We also talked about this before.

Then this class is obviously serving the request of BookController. Obviously, BookController needs to be changed.
First, change the getAll query function code to this

@GetMapping
public ResultUtil getAll() {
    
    
    List<book> BookList = IBookService.list();
    int state = BookList.isEmpty()?500:200;
    String message = BookList.isEmpty()?"未获取到对应记录":"操作成功";
    ResultUtil ResultUtil = new ResultUtil(state,BookList,message);
    return ResultUtil;
}

First, we return a value of the ResultUtil object type as the return value of the method.
Then we define BookList to receive IBookService.list(); the obtained List collection is
then assigned to the state and message with the ternary operator. BookList.isEmpty() is to judge whether the List collection has Didn’t get
it, and finally injected the three fields we processed into the ResultUtil object through the construction method and returned it.
Let’s test it in Postman,
insert image description here
insert image description here
it’s perfect

save Add the code and modify it as follows

@PostMapping
public ResultUtil save(@RequestBody book book){
    
    
    boolean verify = IBookService.save(book);
    int state = verify?200:500;
    String message = verify?"操作成功":"系统异常";
    ResultUtil ResultUtil = new ResultUtil(state,book,message);
    return ResultUtil;
}

Because the method he added originally returns boolean, we directly use it to judge whether it is successful because he has no data, so we directly
return the data he added to him . After adding it, the modification is naturally changed to this
insert image description here


insert image description here

@PutMapping
public ResultUtil pudata(@RequestBody book book){
    
    
    boolean verify = IBookService.updateById(book);
    int state = verify?200:500;
    String message = verify?"操作成功":"系统异常";
    ResultUtil ResultUtil = new ResultUtil(state,book,message);
    return ResultUtil;
}

The results of Postman operation are as follows.
insert image description here
We see that
insert image description here
the data in the database is still successfully modified.
Then delete this time. Let’s delete a piece of data that does not exist.
Let’s change the method first.

@DeleteMapping("{id}")
public ResultUtil delete(@PathVariable Integer id){
    
    
    boolean verify = IBookService.removeById(id);
    int state = verify?200:500;
    String message = verify?"操作成功":"系统异常";
    ResultUtil ResultUtil = new ResultUtil(state,null,message);
    return ResultUtil;
}

Or use Boolean connection to judge whether the operation is successful or not, but the deletion does not involve any data, so we directly give null to
Postman, let’s give a non-existent id 15,
the running results are as follows,
insert image description here
we can see that there is no problem with our return value processing

Query a single data according to id We write like this

@GetMapping("{id}")
public ResultUtil getById(@PathVariable Integer id){
    
    
    book book = IBookService.getById(id);
    int state = book != null?200:500;
    String message = book != null?"操作成功":"系统异常";
    ResultUtil ResultUtil = new ResultUtil(state,book,message);
    return ResultUtil;
}

The running results are as follows
insert image description here
and then the query method of pagination

@GetMapping("/page")
public ResultUtil getPage(@RequestParam int page,int pageSize) {
    
    
    IPage<book> BookPage = IBookService.getPage(page, pageSize);
    int state = BookPage != null?200:500;
    String message = BookPage != null?"操作成功":"系统异常";
    ResultUtil ResultUtil = new ResultUtil(state,BookPage,message);
    return ResultUtil;
}

The idea itself is the same.
Postman runs the code as follows
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131430416