前后端返回数据统一示例

package com.itheima.controller;


//此类表示和前端的数据协议
public class Reoult {
    private  Object data;
    private  Integer code;
    private  String massge;

    public Reoult(Integer integer) {
    }

    public Reoult( Integer code,Object data) {
        this.data = data;
        this.code = code;
    }

    public Reoult(Integer code,Object data, String massge) {
        this.data = data;
        this.code = code;
        this.massge = massge;
    }

    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 getMassge() {
        return massge;
    }

    public void setMassge(String massge) {
        this.massge = massge;
    }
}

package com.itheima.controller;


//表示成功失败的状态码
public class Code {
    //1表示成功
    public static final Integer SAVE_OK=2001;
    public static final Integer UPDATE_OK=2001;
    public static final Integer DELETE_OK=2001;
    public static final Integer GET_OK=2001;

    //0表示成功
    public static final Integer SAVE_ERR=2000;
    public static final Integer UPDATE_ERR=2000;
    public static final Integer DELETE_ERR=2000;
    public static final Integer GET_ERR=2000;
}

package com.itheima.controller;

import com.itheima.dao.userDao;
import com.itheima.service.Userservice;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class userController {
    @Autowired
    private Userservice userservice;

    @PostMapping
    public Reoult save(@RequestBody userDao userDao) {
        boolean flag = userservice.save(userDao);
        return new Reoult(flag ? Code.SAVE_OK : Code.SAVE_ERR,flag);
    }

    @PutMapping
    public Reoult update(@RequestBody userDao userDao) {
        boolean flag = userservice.update(userDao);
        return new Reoult(flag ? Code.UPDATE_OK: Code.UPDATE_ERR,flag);
    }

    @Delete("/{id}")
    public Reoult delete(@PathVariable Integer id) {
        boolean flag = userservice.delete(id);
        return new Reoult(flag ? Code.DELETE_OK: Code.DELETE_ERR,flag);

    }

    @GetMapping("/{id}")
    public Reoult getbyid(@PathVariable Integer id) {
        userDao userDao = userservice.getbyid(id);
        Integer code = userDao !=null ?Code.GET_OK :Code.GET_ERR;
        String massge = userDao !=null ? "":"查询失败,请重试";
        return new Reoult(code,userDao,massge);
    }

    @GetMapping
    public Reoult getall() {
        List<userDao> getall = userservice.getall();
        Integer code = getall !=null ?Code.GET_OK :Code.GET_ERR;
        String massge = getall !=null ? "":"查询失败,请重试";
        return new Reoult(code,getall,massge);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值