REST接口返回数据封装--工具类

前言

最近刚到公司实习,开始入手公司的框架,这种框架的策略模式还是我第一次接触。发现框架里面存在不少问题,其中有一个常用的工具类,就是对后端返回给前端的数据进行封装,总的来说这个工具类也是规规矩矩按常规操作来,但是这个工具类的问题我猜是以前的开发者遗留下来的,我在原来工具类的基础上进行了修改和完善。

我这里用一个简单的SpringBoot项目作为示例。

1.常量类

package com.chen.mykinthtest.domain;

/**
 * 对常量进行封装,利于后期代码的维护
 *
 * @author chen
 */
public class AppConstant {

    // 文本消息
    public static final String MESSAGE = "message";

    // 单个对象
    public static final String ITEM = "item";

    // 返回的对象列表
    public static final String LIST = "list";

    // 状态码
    public static final String ERROR = "error";

    // 代表执行成功
    public static int OK = 0;

    // 代表执行失败
    public static int FAIL = 1;
}

2.封装REST数据

package com.chen.mykinthtest.restful;

import com.chen.mykinthtest.domain.AppConstant;

import java.util.HashMap;
import java.util.List;

/**
 * REST 接口返回数据
 *
 * @author chen
 */
public class RestResponse extends HashMap<String, Object> {

    /**
     * 禁止通过构造函数构造对象,只能通过静态方法获取实例。
     *
     * @see #ok()
     * @see #ok(String)
     * @see #fail()
     * @see #fail(String)
     */
    private RestResponse() {
    }

    /**
     * 设置接口返回的文本消息,属性 key: message
     *
     * @param msg
     * @return
     */
    public RestResponse msg(String msg) {
        this.put(AppConstant.MESSAGE, msg);
        return this;
    }

    /**
     * 设置接口返回的数据对象,属性 key: item
     *
     * @param item
     * @return
     */
    public RestResponse item(Object item) {
        this.put(AppConstant.ITEM, item);
        return this;
    }

    /**
     * 设置接口返回的数据对象列表,属性 key: list
     *
     * @param list
     * @return
     */
    public RestResponse list(List<?> list) {
        this.put(AppConstant.LIST, list);
        return this;
    }

    /**
     * 设置接口返回的数据项,并指定数据项的属性 key
     *
     * @param key
     * @param value
     * @return
     */
    public RestResponse put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 接口执行成功的返回数据,其中属性 error = 0
     *
     * @return
     */
    public static RestResponse ok() {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置文本消息
     *
     * @param msg
     * @return
     */
    public static RestResponse ok(String msg) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).msg(msg);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置对象数据
     *
     * @param item
     * @return
     */
    public static RestResponse ok(Object item) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).item(item);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置列表对象数据
     *
     * @param list
     * @return
     */
    public static RestResponse ok(List<?> list) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).list(list);
        return result;
    }

    /**
     * 接口执行失败的返回数据,其中属性 error = 1
     *
     * @return
     */
    public static RestResponse fail() {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.FAIL);
        return result;
    }

    /**
     * 接口执行失败的返回数据,并设置文本消息,其中属性 error = 1, message = {msg}
     *
     * @param msg
     * @return
     */
    public static RestResponse fail(String msg) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.FAIL).msg(msg);
        return result;
    }

    /**
     * 接口执行失败的返回数据,自定义状态码,其中属性 error = {errcode}
     *
     * @param errcode
     * @return
     */
    public static RestResponse fail(int errcode) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, errcode);
        return result;
    }
}

3.测试用的实体类

package com.chen.mykinthtest.domain.entity;

/**
 * 测试用的实体类
 *
 * @author chen
 */
public class User {

    private String name;
    private int age;
    private char sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}

4.测试控制器

package com.chen.mykinthtest.controller;

import com.chen.mykinthtest.domain.entity.User;
import com.chen.mykinthtest.restful.RestResponse;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 控制器
 *
 * @author chen
 */
@RestController
@RequestMapping("restful")
@CrossOrigin
public class RestfulController {

    /**
     * 执行成功,返回成功的状态码
     *
     * @return
     */
    @RequestMapping("returnOk")
    public RestResponse returnOk() {
        return RestResponse.ok();
    }

    /**
     * 执行成功,返回成功的状态码和文本消息
     *
     * @return
     */
    @RequestMapping("returnOkAndMessage")
    public RestResponse returnOkAndMessage() {
        return RestResponse.ok().msg("执行成功");
    }

    /**
     * 执行失败,返回失败的状态码
     *
     * @return
     */
    @RequestMapping("returnFail")
    public RestResponse returnFail() {
        return RestResponse.fail();
    }

    /**
     * 执行失败,返回失败的状态码和文本消息
     *
     * @return
     */
    @RequestMapping("returnFailAndMessage")
    public RestResponse returnFailAndMessage() {
        return RestResponse.fail().msg("执行失败");
    }

    /**
     * 返回单个对象数据
     *
     * @return
     */
    @RequestMapping("returnItem")
    public RestResponse returnItem() {
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        user.setSex('男');
        return RestResponse.ok().item(user);
    }

    /**
     * 返回对象列表数据
     *
     * @return
     */
    @RequestMapping("returnList")
    public RestResponse returnList() {
        List<User> list = new ArrayList<User>();
        User user1 = new User();
        user1.setName("张三");
        user1.setAge(20);
        user1.setSex('男');
        User user2 = new User();
        user2.setName("李四");
        user2.setAge(22);
        user2.setSex('女');
        list.add(user1);
        list.add(user2);
        return RestResponse.ok().list(list);
    }

    /**
     * 返回其他异常的状态码
     *
     * @return
     */
    @RequestMapping("returnOtherCode")
    public RestResponse returnOtherCode() {
        return RestResponse.fail(3);
    }

}
发布了125 篇原创文章 · 获赞 67 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_42109746/article/details/103946816
今日推荐