首页 > 其他分享 >表现层与前端传输数据协议定义

表现层与前端传输数据协议定义

时间:2022-10-07 16:57:26浏览次数:51  
标签:code 定义 前端 传输数据 Result msg Integer data public

表现层数据封装

整体的思路就是统一前端接受数据格式,创建一个类,用类的属性来封装表现层传输到前端的数据。

package com.rsh.ssm.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;
    }
}
package com.rsh.ssm.controller;

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer UPDATE_OK = 20021;
    public static final Integer DELETE_OK = 20031;
    public static final Integer GET_OK = 20041;
    public static final Integer SAVE_ERR = 20010;
    public static final Integer UPDATE_ERR = 20020;
    public static final Integer DELETE_ERR = 20030;
    public static final Integer GET_ERR = 20040;
}

 

当进行增删改操作时,data属性的值为true或者false;对data的true或false的值无法区分是增删改哪个操作时,加设code属性,用来存储操作的状态码,如20010、20020、20030、20040(自己设置用来区分即可)。msg属性用来封装用户操作的特殊信息,如“修改成功、添加失败等”。

查询单个数据时,data来封装json对象;查询多条数据,data属性的值为json数组。

代码演示:

package com.rsh.ssm.controller;

import com.rsh.ssm.domain.TbBook;
import com.rsh.ssm.service.TbBookService;
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 TbBookService tbBookService;
    @PostMapping
    public Result save(@RequestBody TbBook book) {
        boolean save = tbBookService.save(book);
        String msg = save ? "添加成功" : "添加失败";
        return new Result(save,save?Code.SAVE_OK:Code.SAVE_ERR,msg);
    }
    @PutMapping
    public Result update(@RequestBody TbBook book) {
        boolean update = tbBookService.update(book);
        String msg = update ? "修改成功" : "修改失败";
        return new Result(update,update?Code.UPDATE_OK:Code.UPDATE_ERR,msg);
    }
    @DeleteMapping("/{id}")
    public Result deleteById( @PathVariable Integer id) {
        boolean delete = tbBookService.deleteById(id);
        return new Result(delete,delete?Code.DELETE_OK:Code.DELETE_ERR);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        TbBook tbBook = tbBookService.getById(id);
        Integer integer = tbBook != null ? Code.GET_OK : Code.GET_ERR;
        String msg = tbBook!= null?"查询成功":"查询失败,请重试";
        return new Result(tbBook,integer,msg);
    }
    @GetMapping
    public Result getAll() {
        System.out.println("book getAll is running");
        List<TbBook> all = tbBookService.getAll();
        Integer integer = all != null ? Code.GET_OK : Code.GET_ERR;
        String msg = all!= null?"查询成功":"查询失败,请重试";
        return new Result(all,integer,msg);
    }

}

数据库表信息展示

 

 PostMan模拟:http://localhost/books/100

 

 http://localhost/books/1

 

 

 

 

 

id为6的图书信息本来为空的,现在已经修改成功了

 

标签:code,定义,前端,传输数据,Result,msg,Integer,data,public
From: https://www.cnblogs.com/20203923rensaihang/p/16760022.html

相关文章