Thrift框架学习-定义thrift文件

xx.thrift

thrift支持的数据类型

/**
 * The first thing to know about are types. The available types in Thrift are:
 *
 *  bool        Boolean, one byte
 *  byte        Signed byte
 *  i16         Signed 16-bit integer 
 *  i32         Signed 32-bit integer
 *  i64         Signed 64-bit integer
 *  double      64-bit floating point value
 *  string      String
 *  map<t1,t2>  Map from one type to another
 *  list<t1>    Ordered list of one type
 *  set<t1>     Set of unique elements of one type
 *
 * Did you also notice that Thrift supports C style comments?

 */

对应的java对象类型

/**
* bool - Boolean
* i16 - Short
* i32 - Integer
* i64 - Long
* double - Double
* string - String
* map<t1,t2> - Map
* list<t1> - List<>
* set<t1> - Set<>
**/

Integer类型的list - list
自定义bean类型的list 需要先构建bean

thrift对象定义

枚举enum

enum DemoEnum {
        FULL=1, PART=2
}

对应java枚举:
重写getValue是为了前端传值进来我们专成对应枚举,默认是从0开始,可以指定FULL=1
这样编译的enum是从1开始的
由thrift文件转为java的enum如下:

public enum DemoEnum implements org.apache.thrift.TEnum {
  FULL(1),
  PART(2);

  private final int value;

  private DemoEnum(int value) {
    this.value = value;
  }

  /**
   * Get the integer value of this enum value, as defined in the Thrift IDL.
   */
  public int getValue() {
    return value;
  }

  /**
   * Find a the enum type by its integer value, as defined in the Thrift IDL.
   * @return null if the value is not found.
   */
  public static CompensateStatus findByValue(int value) { 
    switch (value) {
      case 1:
        return FULL;
      case 2:
        return PART;
      default:
        return null;
    }
  }
}

我们自己定义的enum

public enum DemoEnum implements TEnum{

    FULL(1, "full"),
    PART(2, "part");

    private int code;
    private String desc;

    DemoEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public int getValue() {
        return code;
    }

    public static RefundType getByCode(int code) {
        for (RefundType e : RefundType.values()) {
            if (e.getCode() == code) {
                return e;
            }
        }
        return null;
    }


}
public interface TEnum {
    int getValue();
}

实体 bean

required 必传 optional 可选

struct DemoBean {
        1: required string a;
        2: required i32 b;
        3: required i32 c;
        4: optional i64 d;
        5: optional i64 e;
        6: optional string f;
}

对应java类

@ThriftStruct
public class DemoBean {

    private String a;
    private Integer b;
    private Integer c;
    private Long d;
    private Long e;
    private String f;

    @ThriftField(value = 1, requiredness = ThriftField.Requiredness.REQUIRED)
    public String getA() {
        return a;
    }
    @ThriftField
    public void setA(String a) {
        this.a = a;
    }
    @ThriftField(value = 2, requiredness = ThriftField.Requiredness.REQUIRED)
    public Integer getB() {
        return b;
    }
    @ThriftField
    public void setB(Integer b) {
        this.b = b;
    }
    //...省略get/set方法

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DemoBean{");
        sb.append("a='").append(a).append('\'');
        sb.append(", b='").append(b).append('\'');
        sb.append(", c='").append(c).append('\'');
        sb.append(", d=").append(d).append('\'');
        sb.append(", e=").append(e).append('\'');
        sb.append(", f=").append(f).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

接口

struct Response {
		//异常
        9999: required ArcticServiceError error;
}
//定义请求bean
struct DemoBean {
        1: required string a;
        2: required i32 b;
        3: required i32 c;
        4: optional i64 d;
        5: optional i64 e;
        6: optional string f;
}
service ArcticThriftService {
        Response query(1:  DemoBean req);
}

对应java类

@ThriftService
public interface ArcticThriftService {

    @ThriftMethod
    Response query(DemoBean req) throws TException;
}

猜你喜欢

转载自blog.csdn.net/lbh199466/article/details/105638951