Thrift(二):Thrift注解

江佐
2023-12-01

目录

1、Thrift注解开发介绍

2、注解Demo

1、服务接口

2、各个类

3、枚举

3、注解和IDL规范

4、编码规范


​​​​​​​

1、Thrift注解开发介绍

Java服务开发流程上,取消IDL。

注解方式和IDL方式一样,不支持泛型。

使用注解开发方式时,应避免IDL和注解混用。

2、注解Demo

1、服务接口

@ThriftService
public interface TestService {

    @ThriftMethod
    public TestResponse method1(TestRequest testRequest) throws TException;

    @ThriftMethod
    public Long method2(int i) throws TException;

    @ThriftMethod(exception = {@ThriftException(type = TestException.class, id=1)})
    public String method3() throws TestException, TException;

}

2、各个类

@ThriftStruct
public class TestRequest {

    private Integer userId;
    private String message;
    private Integer seqId;

    @ThriftConstructor
    public TestRequest(Integer userId, String message, Integer seqId) {
        this.userId = userId;
        this.message = message;
        this.seqId = seqId;
    }

    @ThriftField(value = 1,requiredness = ThriftField.Requiredness.REQUIRED)
    public Integer getUserId() {
        return userId;
    }

    @ThriftField
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @ThriftField(2)
    public String getMessage() {
        return message;
    }

    @ThriftField
    public void setMessage(String message) {
        this.message = message;
    }

    @ThriftField(value = 3,requiredness = ThriftField.Requiredness.OPTIONAL)
    public Integer getSeqId() {
        return seqId;
    }

    @ThriftField
    public void setSeqId(Integer seqId) {
        this.seqId = seqId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestRequest that = (TestRequest) o;
        return Objects.equals(userId, that.userId) &&
                Objects.equals(message, that.message) &&
                Objects.equals(seqId, that.seqId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, message, seqId);
    }

    @Override
    public String toString() {
        return "TestResponse{" +
                "userId=" + userId +
                ", message='" + message + '\'' +
                ", seqId=" + seqId +
                '}';
    }
}
@ThriftStruct
public class TestResponse {

    private Integer userId;
    private String message;
    private Integer seqId;

    @ThriftConstructor
    public TestResponse(Integer userId, String message, Integer seqId) {
        this.userId = userId;
        this.message = message;
        this.seqId = seqId;
    }

    @ThriftField(value = 1,requiredness = ThriftField.Requiredness.REQUIRED)
    public Integer getUserId() {
        return userId;
    }

    @ThriftField
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @ThriftField(2)
    public String getMessage() {
        return message;
    }

    @ThriftField
    public void setMessage(String message) {
        this.message = message;
    }

    @ThriftField(value = 3,requiredness = ThriftField.Requiredness.OPTIONAL)
    public Integer getSeqId() {
        return seqId;
    }

    @ThriftField
    public void setSeqId(Integer seqId) {
        this.seqId = seqId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestResponse that = (TestResponse) o;
        return Objects.equals(userId, that.userId) &&
                Objects.equals(message, that.message) &&
                Objects.equals(seqId, that.seqId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, message, seqId);
    }

    @Override
    public String toString() {
        return "TestResponse{" +
                "userId=" + userId +
                ", message='" + message + '\'' +
                ", seqId=" + seqId +
                '}';
    }
}
@ThriftStruct
public class TestException extends AbstractThriftException {

    private String message;

    @ThriftConstructor
    public TestException(String message) {
        this.message = message;
    }

    @Override
    @ThriftField(1)
    public String getMessage() {
        return message;
    }

    @ThriftField
    public void setMessage(String message) {
        this.message = message;
    }
}

3、枚举

@ThriftEnum
public enum ThriftAnnotatedEnum {

    FIRST_VALUE("first",0),
    SECOND_VALUE("second",1);

    private String description;
    //直接在枚举类定义整数类型的成员变量用于标识,int类型这里不能为负数
    private int intValue;



    ThriftAnnotatedEnum(String description,int intValue) {
        this.description = description;
        this.intValue = intValue;
    }

    @ThriftEnumValue
    public int getIntValue() {
        return intValue;
    }
}

3、注解和IDL规范

service       --     @ThriftService

struct          --     @ThriftStruct

struct属性   --     @ThriftField

enum          --      @ThriftEnumValue

4、编码规范

在swift中service一般以接口的形式存在

  • 整个类用@ThriftService注解
  • 方法用@ThriftMethod注解
  • 异常用@ThriftMethod(exception = {@ThriftException(type = InvalidException.class, id = 1)})
  • @ThriftStruct注解类、枚举、异常
  • 成员变量的get方法用@ThriftField(1)注解,括号里面的表示字段的ID,这个在thrift文件里有体现。set方法不需要ID
  • 构造器用@ThriftConstructor注解
  • 枚举用@ThriftEnum注解

 类似资料: