• Java返回结果封装,SpringBoot返回结果封装


    ================================

    ©Copyright 蕃薯耀 2020-01-09

    https://www.cnblogs.com/fanshuyao/

    当后面需要返回json结果给前端时,往往要写很多代码,不利于代码重用,所以封装一个返回结果的bean,直接调用,这样方便简洁、高效率、代码统一美观。

    一、返回结果bean封装

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.szpl.gtkj.util.JsonUtil;
    
    public class Result {
        
        public static final String SUCCESS = "操作成功。";
        public static final String FAILURE = "操作失败!";
        
        private boolean result;
        private String msg;
        private Object datas;
        
        private Result() {}
        
        public static Result ok() {
            return Result.ok(SUCCESS);
        }
        
        public static Result ok(String msg) {
            return Result.ok(msg, null);
        }
        
        public static Result ok(Object datas) {
            return Result.ok(SUCCESS, datas);
        }
        
        public static Result ok(String msg, Object datas) {
            Result result = new Result();
            result.setResult(true);
            result.setMsg(msg);
            result.setDatas(datas);
            return result;
        }
        
        public static Result fail() {
            return Result.fail(FAILURE);
        }
        
        public static Result fail(String msg) {
            return Result.fail(msg, null);
        }
        
        public static Result fail(Object datas) {
            return Result.fail(FAILURE, datas);
        }
        
        public static Result fail(String msg, Object datas) {
            Result result = new Result();
            result.setResult(false);
            result.setMsg(msg);
            result.setDatas(datas);
            return result;
        }
        
        public boolean isResult() {
            return result;
        }
        public void setResult(boolean result) {
            this.result = result;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
        public Object getDatas() {
            return datas;
        }
        public void setDatas(Object datas) {
            this.datas = datas;
        }
        
    
        public static void main(String[] args) {
            System.out.println(JsonUtil.obj2String(Result.ok()));
            System.out.println(JsonUtil.obj2String(Result.ok("haha")));
            System.out.println(JsonUtil.obj2String(Result.ok("haha", "aaa")));
            
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("aa", "111");
            map.put("bb", "22");
            map.put("ccc", "33");
            System.out.println(JsonUtil.obj2String(Result.ok("haha", map)));
            
            List<String> list = new ArrayList<String>();
            list.add("aaaaaaaa");
            list.add("bbbbbb");
            list.add("cccccccccccc");
            list.add("ddddddddddd");
            System.out.println(JsonUtil.obj2String(Result.ok("haha", list)));
            
            System.out.println(JsonUtil.obj2String(Result.fail()));
            System.out.println(JsonUtil.obj2String(Result.fail("失败了")));
            System.out.println(JsonUtil.obj2String(Result.fail(list)));
            System.out.println(JsonUtil.obj2String(Result.fail("失败了", map)));
        }
        
        
    }

    二、使用方法:

    1、返回成功:

    return Result.ok();

    重构的方法:

    public static Result ok() {
            return Result.ok(SUCCESS);
        }
        
        public static Result ok(String msg) {
            return Result.ok(msg, null);
        }
        
        public static Result ok(Object datas) {
            return Result.ok(SUCCESS, datas);
        }
        
        public static Result ok(String msg, Object datas) {
            Result result = new Result();
            result.setResult(true);
            result.setMsg(msg);
            result.setDatas(datas);
            return result;
        }

    2、返回失败:

    return Result.fail();

    重构的方法:

    public static Result fail() {
            return Result.fail(FAILURE);
        }
        
        public static Result fail(String msg) {
            return Result.fail(msg, null);
        }
        
        public static Result fail(Object datas) {
            return Result.fail(FAILURE, datas);
        }
        
        public static Result fail(String msg, Object datas) {
            Result result = new Result();
            result.setResult(false);
            result.setMsg(msg);
            result.setDatas(datas);
            return result;
        }

    二、使用示例:

    在Controller声明@RestController

    @RestController
    @RequestMapping("/xxx")
    public class XxxController{
    ……
    }

    结果返回的是Result对象bean

    @RequestMapping("/uploadFiles")
        public Result uploadFiles(HttpServletRequest request, HttpServletResponse response, 
                @RequestParam("files") MultipartFile[] multipartFiles) throws FileNotFoundException, IOException, ValidationException{
            
            List<FileBean> fileBeans = this.saveFiles(request, response, multipartFiles);
            
            log.info("fileBeans = " + JsonUtil.obj2String(fileBeans));
            
            return Result.ok(fileBeans);
            
        }
    @RequestMapping("/save")
    public Result save(HttpServletRequest request, HttpServletResponse response, 
                @RequestParam(value="files", required=true) MultipartFile[] multipartFiles) throws Exception {
    
        return Result.ok();
            
    }

    (如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!) 

    ================================

    ©Copyright 蕃薯耀 2020-01-09

    https://www.cnblogs.com/fanshuyao/

  • 相关阅读:
    HashMap源码解析
    深入理解Java字符串
    Netty粘包、半包
    Netty源码分析-Future和Promise
    Lock简介
    一、Netty中的EventLoop
    对象实例化内存布局与访问定位
    运行时数据区概述及线程
    TCP三次握手和四次挥手
    Redis线程IO模型-Redis 单线程为什么还能这么快?
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/12169792.html
Copyright © 2020-2023  润新知