使用Api微服务工程进行集中管理

image.png

使用接口形式将其从项目脱离出来自形成一个微服务。可以保证接口不冗杂,因为接口必须实现,而且,也便于工程师管理查看接口有哪些,入参有哪些

5.2 定义接口

接口与继承助力响应返回

自定义返回数据接口:

public interface Response {
    public static final boolean SUCCESS = true;
    public static final int SUCCESS_CODE = 10000;
}

实现response接口:

package com.xuecheng.framework.model.response;
 
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
 
/**
 * @Author: mrt. * @Description: * @Date:Created in 2018/1/24 18:33. * @Modified By: */
 
@Data
@ToString
@NoArgsConstructor
public class ResponseResult implements Response {
 
    //操作是否成功
    boolean success = SUCCESS;
 
    //操作代码
    int code = SUCCESS_CODE;
 
    //提示信息
    String message;
 
    public ResponseResult(ResultCode resultCode){
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.message = resultCode.message();
    }
 
    public static ResponseResult SUCCESS(){
        return new ResponseResult(CommonCode.SUCCESS);
    }
    public static ResponseResult FAIL(){
        return new ResponseResult(CommonCode.FAIL);
    }
 
}

注意,这里加了无参构造注解@NoArgsConstructor: 表示默认返回我们在respone接口定义的final常量,返回成功,返回码10000

最后继承ResponeResult:

 
package com.xuecheng.framework.model.response;
 
@Data
@ToString
public class QueryResponseResult extends ResponseResult {
 
    QueryResult queryResult;
 
    public QueryResponseResult(ResultCode resultCode,QueryResult queryResult){
        super(resultCode);
       this.queryResult = queryResult;
    }
 
}

ResultCode接口:

package com.xuecheng.framework.model.response;
 
/**
 * Created by mrt on 2018/3/5. *
 *
 * 10000-- 通用错误代码
 * 22000-- 媒资错误代码
 * 23000-- 用户中心错误代码
 * 24000-- cms错误代码
 * 25000-- 文件系统
 */
public interface ResultCode {
    //操作是否成功,true为成功,false操作失败
    boolean success();
    //操作代码
    int code();
    //提示信息
    String message();
 
}

然后枚举类commoncode实现Resultcode:

package com.xuecheng.framework.model.response;
 
import lombok.ToString;
 
/**
 * @Author: mrt. * @Description: * @Date:Created in 2018/1/24 18:33. * @Modified By: */
@ToString
public enum CommonCode implements ResultCode{
 
    SUCCESS(true,10000,"操作成功!"),
    FAIL(false,11111,"操作失败!"),
    UNAUTHENTICATED(false,10001,"此操作需要登陆系统!"),
    UNAUTHORISE(false,10002,"权限不足,无权操作!"),
    SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");
//    private static ImmutableMap<Integer, CommonCode> codes ;
    //操作是否成功
    boolean success;
    //操作代码
    int code;
    //提示信息
    String message;
    private CommonCode(boolean success,int code, String message){
        this.success = success;
        this.code = code;
        this.message = message;
    }
 
    @Override
    public boolean success() {
        return success;
    }
    @Override
    public int code() {
        return code;
    }
 
    @Override
    public String message() {
        return message;
    }
 
 
}