接口统一返回

import com.google.common.base.Objects;

import java.io.Serializable;

/**
 * dubbo接口统一返回结果
 * 使用isSuccess判断调用是否成功 ,如果为true,则可以调用getResult,如果为false,则调用errorCode来获取出错信息
 * <p>
 * 1、isSuccess         判断调用是否成功
 * 2、getResult         获取调用结果集
 * 3、setResult         设置调用结果集
 * 4、getErrorCode      获取错误码
 * 5、setErrorCode      设置错误码
 * 6、getErrorMsg       获取错误描述
 * 7、setErrorMsg       设置错误描述
 * </p>
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 8350327877975282483L;

    /**
     * 调用是否成功
     */
    private boolean success;

    /**
     * 调用结果集
     */
    private T result;

    /**
     * 错误码
     */
    private String errorCode;

    /**
     * 错误描述
     */
    private String errorMsg;

    /**
     * 默认构造方法
     */
    public Result() {
    }

    /**
     * 直接构造成功的返回
     * @param result
     */
    public Result(T result) {

        this.success = true;
        this.result = result;
    }

    /**
     * 直接构造失败的返回
     * @param errorCode     错误码
     * @param errorMsg      错误描述
     */
    public Result(String errorCode, String errorMsg) {

        this.success = false;
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 判断调用是否成功
     * @return
     */
    public boolean isSuccess() {

        return success;
    }

    /**
     * 获取调用结果集
     * @return
     */
    public T getResult() {

        return result;
    }

    /**
     * 设置调用结果集
     * @param result    结果集
     */
    public void setResult(T result) {

        success = true;
        this.result = result;
    }

    /**
     * 获取错误码
     * @return
     */
    public String getErrorCode() {

        return errorCode;
    }

    /**
     * 设置错误码
     * @param errorCode     错误码
     */
    public void setErrorCode(String errorCode) {

        this.success = false;
        this.errorCode = errorCode;
    }

    /**
     * 获取错误描述
     * @return
     */
    public String getErrorMsg() {

        return errorMsg;
    }

    /**
     * 设置错误描述
     * @param errorMsg      错误描述
     */
    public void setErrorMsg(String errorMsg) {

        this.errorMsg = errorMsg;
    }

    /**
     * 重写toString方法
     * @return
     */
    @Override
    public String toString() {

        return Objects.toStringHelper(this)
                .add("success", success)
                .add("result", result)
                .add("errorCode", errorCode)
				.add("errorMsg", errorMsg)
                .omitNullValues()
                .toString();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Laravel 中,可以通过自定义中间件来实现 API 统一返回格式。具体步骤如下: 1. 创建中间件文件,例如 `ApiFormatMiddleware.php`,并在 `app/Http/Kernel.php` 文件的 `$middlewareGroups` 属性中添加中间件: ```php protected $middlewareGroups = [ // ... 'api' => [ // ... \App\Http\Middleware\ApiFormatMiddleware::class, ], ]; ``` 2. 在 `ApiFormatMiddleware.php` 中,编写中间件逻辑,统一处理 API 返回格式: ```php <?php namespace App\Http\Middleware; use Closure; class ApiFormatMiddleware { public function handle($request, Closure $next) { $response = $next($request); if ($response->exception instanceof \App\Exceptions\ApiException) { // 处理自定义 API 异常 return response()->json([ 'code' => $response->getStatusCode(), 'message' => $response->exception->getMessage(), 'data' => null, ]); } // 统一处理成功响应 return response()->json([ 'code' => 0, 'message' => 'success', 'data' => $response->original, ]); } } ``` 3. 在 API 控制器中,根据业务逻辑处理返回结果: ```php <?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { $users = User::all(); return $this->success($users); } public function store() { // ... if ($error) { throw new \App\Exceptions\ApiException('创建用户失败'); } return $this->success($user); } protected function success($data) { return response()->json($data); } } ``` 这样,所有 API 请求都会经过 `ApiFormatMiddleware` 中间件,返回格式会被统一处理。同时在业务逻辑中,可以通过抛出 `ApiException` 异常来处理自定义的 API 异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值