Spring MVC 返回统一JSON格式

Spring MVC 返回统一JSON格式

    • 格式
    • 1、创建一个存放公共返回码的枚举类型
    • 2、创建一个Result类封装返回数据
    • 3、分页展示工具类
    • 4、案例展示

格式

Spring MVC 返回统一JSON格式_第1张图片

1、创建一个存放公共返回码的枚举类型

/**
 * 公共的返回码
 *      返回码code:
 *          成功:10000
 *          失败:10001
 *          未登录:10002
 *          未授权:10003
 *          抛出异常:99999
 */
public enum  ResultCode {
     

    SUCCESS(true,10000,"操作成功!"),
    //---系统错误返回码-----
    FAIL(false,10001,"操作失败"),
    UNAUTHENTICATED(false,10002,"您还未登录"),
    UNAUTHORISE(false,10003,"权限不足"),
    SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!"),

    //---用户操作返回码  2xxxx----
    MOBILEORPASSWORDERROR(false,20001,"用户名或密码错误");
    //---企业操作返回码  3xxxx----
    //---权限操作返回码----
    //---其他操作返回码----

    //操作是否成功
    boolean success;
    //操作代码
    int code;
    //提示信息
    String message;

    ResultCode(boolean success,int code, String message){
     
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public boolean success() {
     
        return success;
    }

    public int code() {
     
        return code;
    }

    public String message() {
     
        return message;
    }

}

2、创建一个Result类封装返回数据

@Data //提供getter、setter方法
@NoArgsConstructor //无参构造
public class Result {
     

    private boolean success;
    private Integer code;
    private String message;
    private Object data;

	//不需要返回数据时使用
    public Result(ResultCode code) {
     
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
    }

    public Result(ResultCode code,Object data) {
     
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
        this.data = data;
    }

    public Result(Integer code,String message,boolean success) {
     
        this.code = code;
        this.message = message;
        this.success = success;
    }
	
	/*
	* 调用ResultCode类封装常用的返回数据
	*/
    public static Result SUCCESS(){
     
        return new Result(ResultCode.SUCCESS);
    }

    public static Result ERROR(){
     
        return new Result(ResultCode.SERVER_ERROR);
    }

    public static Result FAIL(){
     
        return new Result(ResultCode.FAIL);
    }
}

3、分页展示工具类

/**
 * 分页
 *      {
 *          “success”:“成功”,
 *          “code”:10000
 *          “message”:“ok”,
 *          ”data“:{
 *              total://总条数
 *              rows ://数据列表
 *          }
 *      }
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult<T> {
     

    private Long total;
    private List<T> rows;

}

4、案例展示

  1. 带有返回数据
@RestController
public class CompanyController {
     

    @Autowired
    private CompanyService companyService;

    @GetMapping("/get/{id}")
    public Result getCompany(@PathVariable("id") Long id) {
     
        Company company = companyService.getById(id);
        return new Result(ResultCode.SUCCESS,company);
    }
}
  1. 无返回数据
 @RestController
public class CompanyController {
     

    @Autowired
    private CompanyService companyService;

    @PostMapping("/add")
    public Result addCompany(@RequestBody Company company) {
     
        companyService.addCompany(company);
        return new Result(ResultCode.SUCCESS);
    }
}
  1. 分页数据
@RestController
public class CompanyController {
     

    @Autowired
    private CompanyService companyService;
    
    @GetMapping("/pageList")
    public Result getCompanies(@RequestParam("page") Long page,
                               @RequestParam("limit") Long limit) {
     
        //创建page对象,传递两个参数
        Page<Company> pageCompany = new Page<>(page,limit);
        //调用方法分页查询(基于mybatis-plus实现,这里不详述)
        companyService.page(pageCompany,null);
        //从pageCompany中获取分页数据
        long total = pageCompany.getTotal();//总记录数
        List<Company> records = pageCompany.getRecords();//当前页数据
        PageResult<Company> pr = new PageResult<>(total,records);
        return new Result(ResultCode.SUCCESS,pr);
    }
}
  1. 返回数据展示
 {
     
    "success": true,
    "code": 10000,
    "message": "操作成功!",
    "data": {
     
        "total": 14,
        "rows": [
            {
     
                "id": 1239786045768237058,
                "name": "dfasf0",
                "managerId": "1231241240",
            },
            {
     
                "id": 1239786045768237058,
                "name": "dfasf1",
                "managerId": "1231241241",
            },
            {
     
                "id": 1239786045768237058,
                "name": "dfasf2",
                "managerId": "1231241242",
            },
            {
     
                "id": 1239786045768237058,
                "name": "dfasf3",
                "managerId": "1231241243",
            }
        ]
    }
}

你可能感兴趣的:(Spring,java,json)