• Spring boot自定义返回实体


    Spring boot自定义返回实体

    定义统一的数据结构能使前后端联调变得非常便捷。

    这里期望返回的数据如下:

    {
        "msg": "成功",
        "status": 200,
        "time": 123456,
        "body": {}
    }
    

    如果是查单条记录,则body对应该记录;如果是查分页记录,对应的body如下

    {//Result
        "msg": "成功", //ResultEnum
        "status": 200, //ResultEnum
        "time": 123456,
        "body": {//ResultPage
        	"list": [],
        	"paging": { //Paging
        	"pageNum": 0,
                "pageSize": 10,
                "total": 0
        	}
        }
    }
    

    按对象层次结构拆解成ResultPagingResultPage以及包装生成Result的ResultEntry和状态ResultEnum

    Result

    package com.lhx.upgrade.entries.result;
    import java.io.Serializable;
    public class Result<T> implements Serializable{
        private String msg;
        private Integer status;
        private Long time;
        private T body;
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
        public Integer getStatus() {
            return status;
        }
        public void setStatus(Integer status) {
            this.status = status;
        }
        public Long getTime() {
            return time;
        }
        public void setTime(Long time) {
            this.time = time;
        }
        public T getBody() {
            return body;
        }
        public void setBody(T body) {
            this.body = body;
        }
        @Override
        public String toString() {
            return "Result [body=" + body + ", msg=" + msg + ", status=" + status + ", time=" + time + "]";
        }
    }
    

    Paging

    package com.lhx.upgrade.entries.result;
    import java.io.Serializable;
    public class Paging implements Serializable{
        private Integer pageNum;
        private Integer pageSize;
        private Long total;
        public Paging() {}
        public Paging(Integer pageNum, Integer pageSize, Long total) {
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.total = total;
        }
        public Integer getPageNum() {
            return pageNum;
        }
        public void setPageNum(Integer pageNum) {
            this.pageNum = pageNum;
        }
        public Integer getPageSize() {
            return pageSize;
        }
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
        public Long getTotal() {
            return total;
        }
        public void setTotal(Long total) {
            this.total = total;
        }
        @Override
        public String toString() {
            return "paging [pageNum=" + pageNum + ", pageSize=" + pageSize + ", total=" + total + "]";
        }
        
    }
    

    ResultPage

    package com.lhx.upgrade.entries.result;
    
    import java.io.Serializable;
    import java.util.List;
    
    import org.springframework.data.domain.Page;
    
    public class ResultPage<T> implements Serializable{
        private List<T> list;
        private Paging paging;
        public ResultPage() {}
        public ResultPage(List<T> list, Paging paging) {
            this.list = list;
            this.paging = paging;
        }
        public ResultPage(Page<T> page) {
            this.list = page.getContent();
            this.paging = new Paging(page.getNumber(), page.getSize(), page.getTotalElements());
        }
        public List<T> getList() {
            return list;
        }
        public void setList(List<T> list) {
            this.list = list;
        }
        public Paging getPaging() {
            return paging;
        }
        public void setPaging(Paging paging) {
            this.paging = paging;
        }
        @Override
        public String toString() {
            return "ResultPage [list=" + list + ", paging=" + paging + "]";
        }   
    }
    

    ResultEnum

    package com.lhx.upgrade.entries.result;
    
    public enum ResultEnum {
        UNKNOWN_ERROR(500,"未知错误"),
        SUCCESS(200,"成功"),
        USER_NOT_EXIST(401,"用户不存在");
    
        private Integer status;
        private String msg;
     
        ResultEnum(Integer status, String msg) {
            this.status = status;
            this.msg = msg;
        }
     
        public Integer getStatus() {
            return status;
        }
     
        public String getMsg() {
            return msg;
        }
    }
    

    ResultEntry

    package com.lhx.upgrade.entries.result;
    import java.io.Serializable;
    
    public class ResultEntry<T> implements Serializable{
        public static <T> Result<T> success(T o) {
            Result<T> result = new Result<T>();
            result.setMsg(ResultEnum.SUCCESS.getMsg());
            result.setStatus(ResultEnum.SUCCESS.getStatus());
            result.setBody(o);
            return result;
        }
        public static <T> Result<T> success() {
            Result<T> result = new Result<T>();
            result.setMsg(ResultEnum.SUCCESS.getMsg());
            result.setStatus(ResultEnum.SUCCESS.getStatus());
            return result;
        }
        public static <T> Result<T> response(ResultEnum enu, T o) {
            Result<T> result = new Result<T>();
            result.setMsg(enu.getMsg());
            result.setStatus(enu.getStatus());
            result.setBody(o);
            return result;
        }
        public static <T> Result<T> response(ResultEnum enu) {
            Result<T> result = new Result<T>();
            result.setMsg(enu.getMsg());
            result.setStatus(enu.getStatus());
            return result;
        }
    }
    
    

    JPA接口案例

    //controller
    @RequestMapping(value = "upgrade/records", method = RequestMethod.GET)
    public Result<ResultPage<UpgradeRecordDTO>> getUpgradeRecords() {
        ResultPage<UpgradeRecordDTO> data = upgradeService.getUpgradeRecords();
        return ResultEntry.success(data);
    }
    //service
    public ResultPage<UpgradeRecordDTO> getUpgradeRecords() {
        Pageable pageable = PageRequest.of(0, 10);
        return new ResultPage<>(recordRepository.findAll(pageable));
    }
    //dao
    @Repository()
    public interface UpgradeRecordRepository extends JpaRepository<UpgradeRecordDTO, Integer>{
     
    }
    

    附录:

    问题1:API分页数据返回给前端时报错。com.lhx.upgrade.entries.result.Paging

    `Failed on call to `getDeclaredMethods()` on class `com.lhx.upgrade.entries.result.Paging`, problem: (java.lang.NoClassDefFoundError) Lone (through reference chain: com.lhx.upgrade.entries.result.Result["body"]->com.lhx.upgrade.entries.result.ResultPage["paging"])`
    

    原因:实体未实现java.io.Serializable。这是一个没有内容的接口,应该只做标记使用,序列化的对象在远程传输时可以在流与对象互相转换。

    敌人总是会在你最不想它出现的地方出现!
  • 相关阅读:
    (转)EDM邮件制作规范完整版
    (转)Gmail,你必须了解的12个邮件编码问题
    说说CakePHP的关联模型之一 基本关联
    HTML5 离线应用程序
    CakePHP模型中使用join的多种写法
    判断浏览器
    Javascript闭包例子
    安装wamp后,其显示目录的图标显示不出来
    underscore.js 分析 第二天
    HTML5心得
  • 原文地址:https://www.cnblogs.com/longhx/p/15741048.html
Copyright © 2020-2023  润新知