zoukankan      html  css  js  c++  java
  • 前后端分离-定义响应格式化数据

    一、统一响应结果实体类:
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import lombok.experimental.Accessors;

    import java.io.Serializable;

    /**
    * @ClassName: Result
    * @Description:
    * @Version: v1.0.0
    * @Author: greatesky
    * @Date: 2019/9/7 11:22
    * Modification History:
    * Date Author Version Description
    * -------------------------------------------------------------
    * 2019/9/7 greatesky v1.0.0 创建
    */
    @ApiModel(value = "统一响应结果")
    @Data
    @Accessors(chain = true)
    public class Result<T> implements Serializable {
    private static final long serialVersionUID = -5336210741362413873L;

    @ApiModelProperty(value = "接口调用是否成功,true = 成功,false = 不成功")
    private Boolean isSuccess;

    @ApiModelProperty(value = "响应结果标识符")
    private String code;

    @ApiModelProperty(value = "响应结果说明")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T data;
    }
    二、响应结果
    **
    * @ClassName: ResultGenerator
    * @Description: Result 生成工具
    * @Version: v1.0.0
    * @Author: greatesky
    * @Date: 2019/9/7 11:27
    * Modification History:
    * Date Author Version Description
    * -------------------------------------------------------------
    * 2019/9/7 greatesky v1.0.0 创建
    */
    public class ResultGenerator<T> {

    public static Result genSuccessResult() {
    return new Result()
    .setCode("200")
    .setMessage("成功")
    .setIsSuccess(true);
    }

    public static <T> Result genSuccessResult(T data) {
    return new Result()
    .setIsSuccess(true)
    .setCode("200")
    .setMessage("成功")
    .setData(data);
    }

    public static Result genSuccessResult(String code, String message) {
    return new Result()
    .setIsSuccess(true)
    .setCode(code)
    .setMessage(message);
    }

    public static <T> Result genSuccessResult(String code, String message, T data) {
    return new Result()
    .setIsSuccess(true)
    .setCode(code)
    .setMessage(message)
    .setData(data);
    }

    public static Result genFailResult() {
    return new Result()
    .setIsSuccess(false)
    .setCode("-1")
    .setMessage("失败");
    }


    public static Result genFailResult(String code, String message) {
    return new Result()
    .setIsSuccess(false)
    .setCode(code)
    .setMessage(message);
    }

    public static <T> Result genFailResult(String code, String retMSg, T data) {
    return new Result()
    .setIsSuccess(false)
    .setCode(code)
    .setMessage(retMSg)
    .setData(data);
    }

    }


    三、RestRes响应数据格式化

    import com.alibaba.fastjson.JSONObject;
    import com.github.pagehelper.PageInfo;
    import org.apache.commons.lang3.time.DateUtils;

    import java.util.Date;

    /**
    * 响应数据格式化
    * 只适用于名为soymilkadmin的前端项目
    *
    * @author yang
    * @create 2019-05-30
    */
    public class RestRes {
    /**
    * @return com.alibaba.fastjson.JSONObject
    * @Description 返回格式化的表格数据
    * @Date 2019/5/30
    * @Param [list]
    **/
    public static JSONObject table(PageInfo pageInfo) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.clear();
    jsonObject.put("content", pageInfo.getList());
    jsonObject.put("totalElements", pageInfo.getTotal());
    return jsonObject;
    }

    /**
    * @return com.alibaba.fastjson.JSONObject
    * @Description 返回
    * @Date 2019/5/30
    * @Param [list]
    **/
    public static <T> JSONObject token(String token, T user) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.clear();
    jsonObject.put("token", token);
    jsonObject.put("user", user);
    return jsonObject;
    }

    /**
    * @return com.alibaba.fastjson.JSONObject
    * @Description
    * @Date 2019/5/30
    * @Param [list]
    **/
    public static <T> JSONObject appToken(String token, int expires_in) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.clear();
    jsonObject.put("access_token", token);
    jsonObject.put("expires_in", expires_in);
    //token类型为app
    jsonObject.put("type", "A");
    return jsonObject;
    }

    public static <T> JSONObject clientToken(String token, int expires_in) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.clear();
    jsonObject.put("token", token);
    long failtime = DateUtils.addSeconds(new Date(), expires_in).getTime();
    jsonObject.put("failtime", failtime);
    //token类型为client
    jsonObject.put("type", "client");
    return jsonObject;
    }

    }


    四、废话少说,看代码运用
    /***
    * @Description 终端设备列表展示
    * @author Fu on 2019/10/22 0022 下午 1:19
    * @param data description
    * @return
    **/
    @ApiOperation(value = "列表展示")
    @GetMapping("/selectTerminals")
    @ResponseBody
    @IgnoreClientToken
    @IgnoreUserToken
    public Result selectTerminalList(@feign.Param("page") Integer page,
    @feign.Param("size") Integer size,
    @feign.Param("companyId") String companyId,
    @feign.Param("asProductPricestrategyId")String asProductPricestrategyId){
    PageInfo pageInfo=service.selectTerminals(page,size,companyId,asProductPricestrategyId);
    return ResultGenerator.genSuccessResult(RestRes.table(pageInfo));
    }


    /**
    * terminalid
    * @return
    * @Description 插入/添加
    * @author Fu on 2019/10/15 0015 下午 6:14
    */
    @ApiOperation(value = "插入")
    @PostMapping("/insertTerminal")
    @ResponseBody
    public Result insertTerminal(@Param("terminalId") String terminalId,
    @Param("coId") String coId,
    @Param("name")String name,
    @Param("phone")String phone){
    sysTerminalManagerService.insertTerminal(terminalId,name,phone,coId);
    return ResultGenerator.genSuccessResult();

    }
     
     
  • 相关阅读:
    [转] JavaScript中的字符串操作
    关于多线程学习的笔记
    [转] jquery 使用方法
    ubuntu下安装tomcat和配置mysql
    [转] 使用CodeViz生成C/C++函数调用关系图
    [转] java中的匿名内部类总结
    [转] Java中继承thread类与实现Runnable接口的区别
    字符串 指针、调试---师傅传授 栈中指针地址 与 堆中数据地址 标准写法
    maven 阿里云节点,速度快
    解决IntelliJ IDEA 创建Maven项目速度慢问题 DarchetypeCatalog
  • 原文地址:https://www.cnblogs.com/xiaoshenke/p/12027375.html
Copyright © 2011-2022 走看看