JAVA WEB开发中REST接口统一返回数据工具类封装

前言

日常JAVA Web项目开发过程中,一般都是基于HTTP+REST风格,Controller接口返回JSON格式数据到前端,前端获取JSON后展示在前端,为了项目的返回数据格式统一,这里总结一下常用Web开发过程中REST接口统一返回数据工具类封装。

一、工具类一RestResponse

  • RestResponse(对内Rest接口-推荐)
package com.example.demo.result;

import java.io.Serializable;

/**
 * REST接口统一返回数据工具类封装RestResponse
 * @param <T>
 */
public class RestResponse<T> implements Serializable {

    private static final long serialVersionUID = 3728877563912075885L;

    private int code;
    private String msg;
    private T data;

    public RestResponse(){

    }

    public RestResponse(int code, String message, T data) {
        this.code = code;
        this.setMsg(message);
        this.data = data;
    }

    public RestResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public RestResponse(int code, String message) {
        this.code = code;
        this.setMsg(message);
    }
 
    /**
     * 成功时-返回data
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success(T data){
        return new RestResponse<T>(200, null, data);
    }

    /**
     * 成功-不返回data
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success(String msg){
        return new RestResponse<T>(200, msg);
    }

    /**
     * 成功-返回data+msg
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success(String msg, T data){
        return new RestResponse<T>(200, msg, data);
    }
 
    /**
     * 失败
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(String msg){
        return new RestResponse<T>(500, msg,null);
    }

    /**
     * 失败-code
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(int code, String msg){
        return new RestResponse<T>(code, msg,null);
    }

 
    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
 
 
    public T getData() {
        return data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "RestResponse{" + "code=" + code + ", msg='" + msg + '\'' +", data=" + data +'}';
    }
}

二、工具类二RpcResponse

  • RpcResponse(对外Rpc接口)
package com.example.oss.result;

import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @desc:
 * @author: cao_wencao
 * @date: 2020-11-06 17:04
 */
@Data
@Accessors(chain = true)
@Builder
public class RpcResponse<T> implements Serializable {
    public static final int CODE_200 = 200;

    public static final int CODE_500 = 500;
    private static final long serialVersionUID = -1559957698621135646L;


    /**
     * 消息 CODE_200
     */
    private int code = CODE_500;

    /**
     * 信息
     */
    private String message;

    /**
     * 数据
     */
    private Object data;


    /**
     * Instantiates a new Api result.
     */
    public RestResponse() {
        super();
    }


    /**
     * Instantiates a new Api result.
     *
     * @param code    the code
     * @param message the message
     * @param data    the data
     */
    public RpcResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * Instantiates a new Api result.
     *
     * @param code    the code
     * @param message the message
     */
    public RpcResponse(int code, String message) {
        this(code, message, null);
    }


    /**
     * 错误
     *
     * @param message the message
     * @return api result
     */
    public static RpcResponse error(String message) {
        return new RpcResponse(CODE_500, message, null);
    }

    /**
     * 错误
     *
     * @param code    the code
     * @param message the message
     * @return api result
     */
    public static RpcResponse error(int code, String message) {
        return new RpcResponse(code, message);
    }

    /**
     * 成功
     *
     * @param message the message
     * @return api result
     */
    public static RpcResponse succee(String message) {
        return new RpcResponse(CODE_200, message);
    }

    /**
     * 成功
     *
     * @param data    the data
     * @param message the message
     * @return api result
     */
    public static RpcResponse succee(Object data, String message) {
        return new RpcResponse(CODE_200, message, data);
    }

    /**
     * 成功
     *
     * @param data the data
     * @return api result
     */
    public RpcResponse success(Object data) {

        return new RpcResponse(CODE_200, null, data);
    }
}

三、工具类三RestResponse

  • RestResponse(对内Rest接口)
package com.example.oss.result;

import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @desc:
 * @author: cao_wencao
 * @date: 2020-11-06 17:04
 */
@Data
@Accessors(chain = true)
@Builder
public class RestResponse implements Serializable {
    public static final int CODE_200 = 200;

    public static final int CODE_500 = 500;
    private static final long serialVersionUID = -1559957698621135646L;


    /**
     * 消息 CODE_200
     */
    private int code = CODE_500;

    /**
     * 信息
     */
    private String message;

    /**
     * 数据
     */
    private Object data;


    /**
     * Instantiates a new Api result.
     */
    public RestResponse() {
        super();
    }


    /**
     * Instantiates a new Api result.
     *
     * @param code    the code
     * @param message the message
     * @param data    the data
     */
    public RestResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * Instantiates a new Api result.
     *
     * @param code    the code
     * @param message the message
     */
    public RestResponse(int code, String message) {
        this(code, message, null);
    }


    /**
     * 错误
     *
     * @param message the message
     * @return api result
     */
    public static RestResponse error(String message) {
        return new RestResponse(CODE_500, message, null);
    }

    /**
     * 错误
     *
     * @param code    the code
     * @param message the message
     * @return api result
     */
    public static RestResponse error(int code, String message) {
        return new RestResponse(code, message);
    }

    /**
     * 成功
     *
     * @param message the message
     * @return api result
     */
    public static RestResponse succee(String message) {
        return new RestResponse(CODE_200, message);
    }

    /**
     * 成功
     *
     * @param data    the data
     * @param message the message
     * @return api result
     */
    public static RestResponse succee(Object data, String message) {
        return new RestResponse(CODE_200, message, data);
    }

    /**
     * 成功
     *
     * @param data the data
     * @return api result
     */
    public RestResponse success(Object data) {

        return new RestResponse(CODE_200, null, data);
    }
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目录 1.简介 2.如何使用示例代码 3.我为什么要学习Wicket? 3.1。我们都喜欢意大利面:-) ... 3.2。面向组件的框架 - 概述 3.3。面向组件的Web开发框架的优点 3.4。Wicket与其他面向组件的框架相比 威克特说“你好世界!” 4.1。Wicket分发和模块 4.2。Wicket应用程序的配置 4.3。HomePage类 4.4。Wicket链接 4.5。摘要 5. Wicket作为页面布局管理器 5.1。页眉,页脚,左侧菜单,内容等... 5.2。这是继承! 5.3。划分et impera! 5.4。使用wicket标记继承:扩展标记 5.5。摘要 6.保持对HTML的控制 6.1。隐藏或禁用组件 6.2。修改标签属性 6.3。生成标记属性“id” 6.4。使用WebMarkupContainer创建内嵌面板 6.5。使用标记片段 6.6。将标题内容添加到最终页面 6.7。在我们的页面/面板使用存根标记 6.8。如何仅渲染组件主体 6.9。用wicket隐藏装饰元素:enclosure标签 6.10。使用Border包围现有标记 6.11。摘要 7.组件生命周期 7.1。组件的生命周期阶段 7.2。组件生命周期的钩子方法 7.3。初始化阶段 7.4。渲染阶段 7.5。删除阶段 7.6。独立舞台 7.7。摘要 8.页面版本控制和缓存 8.1。有状态页面与无状态页面 8.2。有状态页面 8.3。无状态页面 8.4。摘要 9.在请求处理的引擎盖下 9.1。类应用和请求处理 9.2。请求和响应类 9.3。请求处理的“主管” - RequestCycle 9.4。会话类 9.5。异常处理 9.6。摘要 10. Wicket链接和URL生成 10.1。PageParameters 10.2。可收藏的链接 10.3。使用标记wicket自动创建可收藏的链接:链接 10.4。外部链接 10.5。无状态链接 10.6。生成结构清晰的URL 10.7。摘要 11. Wicket模型和表格 11.1。什么是模特? 11.2。IModel和Lambda 11.3。模型和JavaBeans 11.4。Wicket形式 11.5。组件DropDownChoice 11.6。模型链 11.7。可拆卸型号 11.8。在组件使用多个模型 11.9。使用型号! 11.10。摘要 12. Wicket详细说明 12.1。默认表单处理 12.2。表单验证和反馈消息 12.3。输入值转换 12.4。使用JSR 303验证 12.5。使用IFormSubmittingComponent提交表单 12.6。嵌套表格 12.7。多行文字输入 12.8。上传文件 12.9。使用FormComponentPanel创建复杂的表单组件 12.10。无国籍形式 12.11。使用单选按钮和复选框 12.12。使用ListMultipleChoices和Palette选择多个值 12.13。摘要 13.使用继器显示多个项目 13.1。RepeatingView组件 13.2。ListView组件 13.3。RefreshingView组件 13.4。可分页的继器 13.5。摘要 14.组件排队 14.1。标记层次结构和代码 14.2。改进了汽车组件 14.3。组件什么时候出列? 14.4。排队的限制 14.5。摘要 15.与Wicket的国际化 15.1。本土化 15.2。Wicket的本地化 15.3。捆绑查找算法 15.4。组件选择的本地化 15.5。国际化和模型 15.6。摘要 16. Wicket的资源管理 16.1。静态与动态资源 16.2。资源参考 16.3。包资源 16.4。向页眉部分添加资源 16.5。上下文相关资源 16.6。资源依赖性 16.7。使用资源包聚合多个资源 16.8。将JavaScript放在页面正文 16.9。标题贡献者定位 16.10。自定义资源 16.11。安装资源 16.12。Lambda支持 16.13。共享资源 16.14。自定义资源加载 16.15。CssHeaderItem和JavaScriptHeaderItem压缩 16.16。NIO资源 16.17。资源通过模型得出 16.18。摘要 17.与JavaScript集成的示例 17.1。我们想做什么...... 17.2。......以及我们将如何做到这一点 17.3。摘要 18. Wicket高级主题 18.1。通过行为丰富组件 19.使用AJAX 19.1。如何使用AJAX组件和行为 19.2。内置AJAX组件 19.3。内置的AJAX行为 19.4。使用活动指示器 19.5。AJAX请求属性和调用侦听器 19.6。创建自定义AJAX调用侦

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thinkingcao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值