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是爲了前端傳值進來我們專成對應枚舉,默認是ordinal,可以指定FULL=1
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;
}