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

表现层数据封装

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

复制代码
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的图书信息本来为空的,现在已经修改成功了

 

posted @   几人著眼到青衫  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示