SpringBoot统一响应数据格式

  1. 创建枚举类 方便管理返回的 code 码
package com.lu.demo.util;

import lombok.Getter;

@Getter
public enum Enumeration {
    SUCCESS(2000,"成功"),
    ERROR(4000,"错误");
    protected Integer code;
    protected String message;
    Enumeration(Integer code,String message){
        this.code = code;
        this.message =message;
    }
}

  1. 创建统一响应类
package com.lu.demo.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class ResponseFormat {

    private Integer code;
    private String message;
    private Map<String,Object> data = new HashMap<String,Object>();

    private ResponseFormat(){}

    public static ResponseFormat success(){
        ResponseFormat responseFormat = new ResponseFormat();
        responseFormat.setCode(Enumeration.SUCCESS.getCode());
        responseFormat.setMessage(Enumeration.SUCCESS.getMessage());
        return responseFormat;
    }


    public static ResponseFormat error(){
        ResponseFormat responseFormat = new ResponseFormat();
        responseFormat.setCode(Enumeration.ERROR.getCode());
        responseFormat.setMessage(Enumeration.ERROR.getMessage());
        return responseFormat;
    }

    public ResponseFormat code(Integer code){
        this.setCode(code);
        return this;
    }

    public ResponseFormat message(String message){
        this.setMessage(message);
        return this;
    }

    public ResponseFormat data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public ResponseFormat data(Map<String,Object> map){
        this.data = map;
        return this;
    }


}

  1. 使用示例
package com.lu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lu.demo.dto.UserDTO;
import com.lu.demo.entity.User;
import com.lu.demo.service.UserService;
import com.lu.demo.util.ResponseFormat;
import com.lu.demo.vo.user.AllListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("api/v1")
@Valid
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping(value = {"/list/{name}","/list"})
    public ResponseFormat list(@PathVariable(required = false) String name){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(User::getCreateTime);
        queryWrapper.like(!StringUtils.isEmpty(name),User::getUserName,name);
        List<User> userList = userService.list(queryWrapper);
        ArrayList<AllListVO> lists = new ArrayList<>();
        userList.forEach(item->{
            AllListVO vo = new AllListVO();
            BeanUtils.copyProperties(item,vo);
            vo.setTime(item.getCreateTime());
            lists.add(vo);
        });
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",userService.count());
        map.put("items",lists);
        return ResponseFormat.success().data(map);
        //或者  return ResponseFormat.success().data("items",lists);
    }

    @PostMapping("/create")
    public ResponseFormat create(@RequestBody @Validated UserDTO userDTO){
        User user = new User();
        user.setEmail(userDTO.getEmail());
        user.setUserName(userDTO.getName());
        user.setAge(userDTO.getAge());
        boolean result = userService.save(user);
        return ResponseFormat.success();
    }
}

ResponseFormat 类可以通过链式来调用方便快捷
例如:
1、ResponseFormat.success();
2、ResponseFormat.success().code(2000).message(“获取成功”);
3、ResponseFormat.success().data();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值