• 前后端分离-定义响应格式化数据


    一、统一响应结果实体类:
    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();

    }
     
     
  • 相关阅读:
    将不确定变为确定~接口应该是什么
    架构,改善程序复用性的设计~目录(附核心原代码)
    php实战第二十五天
    java一道简单的括号匹配问题
    屈原坐上神十带来了iOS 7
    《别独自用餐》 警句摘录
    php实战第二十四天
    偷了世界的程序员
    变故
    “快排”笔记
  • 原文地址:https://www.cnblogs.com/xiaoshenke/p/12027375.html
Copyright © 2020-2023  润新知