百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 文章教程 > 正文

Thrift 03 | Thrift IDL介绍

xsobi 2025-01-01 23:15 1 浏览

前两篇中简单介绍并使用了IDL,那么IDL支持哪些数据类型,语法又是什么?本篇文章将与大家一起学习。

Java中数据类型可以分为基本数据类型和对象类型。IDL中也类似,包含基本数据类型、容器、结构体等。更准确的说Thrift 类型系统由预定义的基本类型、用户定义的结构、容器类型、异常和服务定义组成。下面会分别进行介绍。

1.基本数据类型

基本数据类型有如下几种:

  • bool: 布尔型,1个字节,取值为true或false。
  • byte: 有符号单字节(8位)整型。
  • i16: 有符号16位整型。
  • i32: 有符号32位整型。
  • i64: 有符号64位整型。
  • double: 64位浮点数。
  • binary: 字节数组。
  • string: 字符串


对应Java的数据类型按照顺序分别是

boolean、byte、short、int、long、double、ByteBuffer、String


2.容器

Thrift 容器是强类型容器,映射到常用编程语言中最常用的容器。它们使用 Java 泛型样式进行注释。有三种容器类型可供选择: list<t1>:t1 类型元素的有序列表。可能包含重复项。 set<t1>:类型为 t1 的无序唯一元素集。 map<t1,t2>:t1 - t2 类型键值对。

容器中使用的类型可以是任何有效的 Thrift 类型(包括结构和异常),但不包括service。


3.结构体

结构体可对应Java的类,但是不能继承,可以进行结构体嵌套。下面结合demo进行说明。

UserResponse.thrift

namespace java ltd.klein.thrift.server.api.resp

include '../param/UserType.thrift'

struct UserResponse {

    /*
     * 请求成功/失败
     */
    1: bool success;

    /*
     * 请求结果信息
     */
    2: string msg;

    3: list<User> users;
}

struct User {
    1: required i32 id;
    2: required string nickName;
    3: required i64 registerTime;
    4: UserType.UserType userType = UserType.UserType.CONSUMER;
    5: binary avatar;
    6: optional i16 age;
    7: optional string desc;
}
  1. namespace:
  2. 编译为Java文件后,代表的是Java的package
  3. include
  4. 引入其他thrift文件定义的struct等类型
  5. struct
  6. 定义一个结构体,编译后对应的是Java的类
  7. 成员变量需要写明顺序
  8. 同一个thrfit文件中,可以定义多个struct,不需要与thrift文件名一致
  9. required 标注的参数,无论是否赋值,在序列化与反序列化时都会被处理
  10. optional 标注的参数,如果没有赋值,在序列化与反序列化时会被忽略

由thrift文件生成的Java文件如下

/**
 * Autogenerated by Thrift Compiler (0.21.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package ltd.klein.thrift.server.api.resp;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.21.0)", date = "2024-11-22")
public class UserResponse implements org.apache.thrift.TBase<UserResponse, UserResponse._Fields>, java.io.Serializable, Cloneable, Comparable<UserResponse> {
  private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("UserResponse");

  private static final org.apache.thrift.protocol.TField SUCCESS_FIELD_DESC = new org.apache.thrift.protocol.TField("success", org.apache.thrift.protocol.TType.BOOL, (short)1);
  private static final org.apache.thrift.protocol.TField MSG_FIELD_DESC = new org.apache.thrift.protocol.TField("msg", org.apache.thrift.protocol.TType.STRING, (short)2);
  private static final org.apache.thrift.protocol.TField USERS_FIELD_DESC = new org.apache.thrift.protocol.TField("users", org.apache.thrift.protocol.TType.LIST, (short)3);

  private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new UserResponseStandardSchemeFactory();
  private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new UserResponseTupleSchemeFactory();

  public boolean success; // required
  public @org.apache.thrift.annotation.Nullable java.lang.String msg; // required
  public @org.apache.thrift.annotation.Nullable java.util.List<User> users; // required

  /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
  public enum _Fields implements org.apache.thrift.TFieldIdEnum {
    SUCCESS((short)1, "success"),
    MSG((short)2, "msg"),
    USERS((short)3, "users");

    private static final java.util.Map<java.lang.String, _Fields> byName = new java.util.HashMap<java.lang.String, _Fields>();

    static {
      for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) {
        byName.put(field.getFieldName(), field);
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, or null if its not found.
     */
    @org.apache.thrift.annotation.Nullable
    public static _Fields findByThriftId(int fieldId) {
      switch(fieldId) {
        case 1: // SUCCESS
          return SUCCESS;
        case 2: // MSG
          return MSG;
        case 3: // USERS
          return USERS;
        default:
          return null;
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, throwing an exception
     * if it is not found.
     */
    public static _Fields findByThriftIdOrThrow(int fieldId) {
      _Fields fields = findByThriftId(fieldId);
      if (fields == null) throw new java.lang.IllegalArgumentException("Field " + fieldId + " doesn't exist!");
      return fields;
    }

    /**
     * Find the _Fields constant that matches name, or null if its not found.
     */
    @org.apache.thrift.annotation.Nullable
    public static _Fields findByName(java.lang.String name) {
      return byName.get(name);
    }

    private final short _thriftId;
    private final java.lang.String _fieldName;

    _Fields(short thriftId, java.lang.String fieldName) {
      _thriftId = thriftId;
      _fieldName = fieldName;
    }

    @Override
    public short getThriftFieldId() {
      return _thriftId;
    }

    @Override
    public java.lang.String getFieldName() {
      return _fieldName;
    }
  }

  // isset id assignments
  private static final int __SUCCESS_ISSET_ID = 0;
  private byte __isset_bitfield = 0;
  public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
  static {
    java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
    tmpMap.put(_Fields.SUCCESS, new org.apache.thrift.meta_data.FieldMetaData("success", org.apache.thrift.TFieldRequirementType.DEFAULT, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BOOL)));
    tmpMap.put(_Fields.MSG, new org.apache.thrift.meta_data.FieldMetaData("msg", org.apache.thrift.TFieldRequirementType.DEFAULT, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
    tmpMap.put(_Fields.USERS, new org.apache.thrift.meta_data.FieldMetaData("users", org.apache.thrift.TFieldRequirementType.DEFAULT, 
        new org.apache.thrift.meta_data.ListMetaData(org.apache.thrift.protocol.TType.LIST, 
            new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, User.class))));
    metaDataMap = java.util.Collections.unmodifiableMap(tmpMap);
    org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(UserResponse.class, metaDataMap);
  }

  public UserResponse() {
  }

  public UserResponse(
    boolean success,
    java.lang.String msg,
    java.util.List<User> users)
  {
    this();
    this.success = success;
    setSuccessIsSet(true);
    this.msg = msg;
    this.users = users;
  }

  /**
   * Performs a deep copy on <i>other</i>.
   */
  public UserResponse(UserResponse other) {
    __isset_bitfield = other.__isset_bitfield;
    this.success = other.success;
    if (other.isSetMsg()) {
      this.msg = other.msg;
    }
    if (other.isSetUsers()) {
      java.util.List<User> __this__users = new java.util.ArrayList<User>(other.users.size());
      for (User other_element : other.users) {
        __this__users.add(new User(other_element));
      }
      this.users = __this__users;
    }
  }

  @Override
  public UserResponse deepCopy() {
    return new UserResponse(this);
  }

  @Override
  public void clear() {
    setSuccessIsSet(false);
    this.success = false;
    this.msg = null;
    this.users = null;
  }

  public boolean isSuccess() {
    return this.success;
  }

  public UserResponse setSuccess(boolean success) {
    this.success = success;
    setSuccessIsSet(true);
    return this;
  }

  public void unsetSuccess() {
    __isset_bitfield = org.apache.thrift.EncodingUtils.clearBit(__isset_bitfield, __SUCCESS_ISSET_ID);
  }

  /** Returns true if field success is set (has been assigned a value) and false otherwise */
  public boolean isSetSuccess() {
    return org.apache.thrift.EncodingUtils.testBit(__isset_bitfield, __SUCCESS_ISSET_ID);
  }

  public void setSuccessIsSet(boolean value) {
    __isset_bitfield = org.apache.thrift.EncodingUtils.setBit(__isset_bitfield, __SUCCESS_ISSET_ID, value);
  }

  @org.apache.thrift.annotation.Nullable
  public java.lang.String getMsg() {
    return this.msg;
  }

  public UserResponse setMsg(@org.apache.thrift.annotation.Nullable java.lang.String msg) {
    this.msg = msg;
    return this;
  }

  public void unsetMsg() {
    this.msg = null;
  }

  /** Returns true if field msg is set (has been assigned a value) and false otherwise */
  public boolean isSetMsg() {
    return this.msg != null;
  }

  public void setMsgIsSet(boolean value) {
    if (!value) {
      this.msg = null;
    }
  }

  public int getUsersSize() {
    return (this.users == null) ? 0 : this.users.size();
  }

  @org.apache.thrift.annotation.Nullable
  public java.util.Iterator<User> getUsersIterator() {
    return (this.users == null) ? null : this.users.iterator();
  }

  public void addToUsers(User elem) {
    if (this.users == null) {
      this.users = new java.util.ArrayList<User>();
    }
    this.users.add(elem);
  }

  @org.apache.thrift.annotation.Nullable
  public java.util.List<User> getUsers() {
    return this.users;
  }

  public UserResponse setUsers(@org.apache.thrift.annotation.Nullable java.util.List<User> users) {
    this.users = users;
    return this;
  }

  public void unsetUsers() {
    this.users = null;
  }

  /** Returns true if field users is set (has been assigned a value) and false otherwise */
  public boolean isSetUsers() {
    return this.users != null;
  }

  public void setUsersIsSet(boolean value) {
    if (!value) {
      this.users = null;
    }
  }

  @Override
  public void setFieldValue(_Fields field, @org.apache.thrift.annotation.Nullable java.lang.Object value) {
    switch (field) {
    case SUCCESS:
      if (value == null) {
        unsetSuccess();
      } else {
        setSuccess((java.lang.Boolean)value);
      }
      break;

    case MSG:
      if (value == null) {
        unsetMsg();
      } else {
        setMsg((java.lang.String)value);
      }
      break;

    case USERS:
      if (value == null) {
        unsetUsers();
      } else {
        setUsers((java.util.List<User>)value);
      }
      break;

    }
  }

  @org.apache.thrift.annotation.Nullable
  @Override
  public java.lang.Object getFieldValue(_Fields field) {
    switch (field) {
    case SUCCESS:
      return isSuccess();

    case MSG:
      return getMsg();

    case USERS:
      return getUsers();

    }
    throw new java.lang.IllegalStateException();
  }

  /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
  @Override
  public boolean isSet(_Fields field) {
    if (field == null) {
      throw new java.lang.IllegalArgumentException();
    }

    switch (field) {
    case SUCCESS:
      return isSetSuccess();
    case MSG:
      return isSetMsg();
    case USERS:
      return isSetUsers();
    }
    throw new java.lang.IllegalStateException();
  }

  @Override
  public boolean equals(java.lang.Object that) {
    if (that instanceof UserResponse)
      return this.equals((UserResponse)that);
    return false;
  }

  public boolean equals(UserResponse that) {
    if (that == null)
      return false;
    if (this == that)
      return true;

    boolean this_present_success = true;
    boolean that_present_success = true;
    if (this_present_success || that_present_success) {
      if (!(this_present_success && that_present_success))
        return false;
      if (this.success != that.success)
        return false;
    }

    boolean this_present_msg = true && this.isSetMsg();
    boolean that_present_msg = true && that.isSetMsg();
    if (this_present_msg || that_present_msg) {
      if (!(this_present_msg && that_present_msg))
        return false;
      if (!this.msg.equals(that.msg))
        return false;
    }

    boolean this_present_users = true && this.isSetUsers();
    boolean that_present_users = true && that.isSetUsers();
    if (this_present_users || that_present_users) {
      if (!(this_present_users && that_present_users))
        return false;
      if (!this.users.equals(that.users))
        return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int hashCode = 1;

    hashCode = hashCode * 8191 + ((success) ? 131071 : 524287);

    hashCode = hashCode * 8191 + ((isSetMsg()) ? 131071 : 524287);
    if (isSetMsg())
      hashCode = hashCode * 8191 + msg.hashCode();

    hashCode = hashCode * 8191 + ((isSetUsers()) ? 131071 : 524287);
    if (isSetUsers())
      hashCode = hashCode * 8191 + users.hashCode();

    return hashCode;
  }

  @Override
  public int compareTo(UserResponse other) {
    if (!getClass().equals(other.getClass())) {
      return getClass().getName().compareTo(other.getClass().getName());
    }

    int lastComparison = 0;

    lastComparison = java.lang.Boolean.compare(isSetSuccess(), other.isSetSuccess());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetSuccess()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.success, other.success);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = java.lang.Boolean.compare(isSetMsg(), other.isSetMsg());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetMsg()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.msg, other.msg);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = java.lang.Boolean.compare(isSetUsers(), other.isSetUsers());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetUsers()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.users, other.users);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    return 0;
  }

  @org.apache.thrift.annotation.Nullable
  @Override
  public _Fields fieldForId(int fieldId) {
    return _Fields.findByThriftId(fieldId);
  }

  @Override
  public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
    scheme(iprot).read(iprot, this);
  }

  @Override
  public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    scheme(oprot).write(oprot, this);
  }

  @Override
  public java.lang.String toString() {
    java.lang.StringBuilder sb = new java.lang.StringBuilder("UserResponse(");
    boolean first = true;

    sb.append("success:");
    sb.append(this.success);
    first = false;
    if (!first) sb.append(", ");
    sb.append("msg:");
    if (this.msg == null) {
      sb.append("null");
    } else {
      sb.append(this.msg);
    }
    first = false;
    if (!first) sb.append(", ");
    sb.append("users:");
    if (this.users == null) {
      sb.append("null");
    } else {
      sb.append(this.users);
    }
    first = false;
    sb.append(")");
    return sb.toString();
  }

  public void validate() throws org.apache.thrift.TException {
    // check for required fields
    // check for sub-struct validity
  }

  private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
    try {
      write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException {
    try {
      // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.
      __isset_bitfield = 0;
      read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private static class UserResponseStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
    @Override
    public UserResponseStandardScheme getScheme() {
      return new UserResponseStandardScheme();
    }
  }

  private static class UserResponseStandardScheme extends org.apache.thrift.scheme.StandardScheme<UserResponse> {

    @Override
    public void read(org.apache.thrift.protocol.TProtocol iprot, UserResponse struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TField schemeField;
      iprot.readStructBegin();
      while (true)
      {
        schemeField = iprot.readFieldBegin();
        if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { 
          break;
        }
        switch (schemeField.id) {
          case 1: // SUCCESS
            if (schemeField.type == org.apache.thrift.protocol.TType.BOOL) {
              struct.success = iprot.readBool();
              struct.setSuccessIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 2: // MSG
            if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
              struct.msg = iprot.readString();
              struct.setMsgIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 3: // USERS
            if (schemeField.type == org.apache.thrift.protocol.TType.LIST) {
              {
                org.apache.thrift.protocol.TList _list0 = iprot.readListBegin();
                struct.users = new java.util.ArrayList<User>(_list0.size);
                @org.apache.thrift.annotation.Nullable User _elem1;
                for (int _i2 = 0; _i2 < _list0.size; ++_i2)
                {
                  _elem1 = new User();
                  _elem1.read(iprot);
                  struct.users.add(_elem1);
                }
                iprot.readListEnd();
              }
              struct.setUsersIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          default:
            org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
        }
        iprot.readFieldEnd();
      }
      iprot.readStructEnd();

      // check for required fields of primitive type, which can't be checked in the validate method
      struct.validate();
    }

    @Override
    public void write(org.apache.thrift.protocol.TProtocol oprot, UserResponse struct) throws org.apache.thrift.TException {
      struct.validate();

      oprot.writeStructBegin(STRUCT_DESC);
      oprot.writeFieldBegin(SUCCESS_FIELD_DESC);
      oprot.writeBool(struct.success);
      oprot.writeFieldEnd();
      if (struct.msg != null) {
        oprot.writeFieldBegin(MSG_FIELD_DESC);
        oprot.writeString(struct.msg);
        oprot.writeFieldEnd();
      }
      if (struct.users != null) {
        oprot.writeFieldBegin(USERS_FIELD_DESC);
        {
          oprot.writeListBegin(new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.STRUCT, struct.users.size()));
          for (User _iter3 : struct.users)
          {
            _iter3.write(oprot);
          }
          oprot.writeListEnd();
        }
        oprot.writeFieldEnd();
      }
      oprot.writeFieldStop();
      oprot.writeStructEnd();
    }

  }

  private static class UserResponseTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
    @Override
    public UserResponseTupleScheme getScheme() {
      return new UserResponseTupleScheme();
    }
  }

  private static class UserResponseTupleScheme extends org.apache.thrift.scheme.TupleScheme<UserResponse> {

    @Override
    public void write(org.apache.thrift.protocol.TProtocol prot, UserResponse struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
      java.util.BitSet optionals = new java.util.BitSet();
      if (struct.isSetSuccess()) {
        optionals.set(0);
      }
      if (struct.isSetMsg()) {
        optionals.set(1);
      }
      if (struct.isSetUsers()) {
        optionals.set(2);
      }
      oprot.writeBitSet(optionals, 3);
      if (struct.isSetSuccess()) {
        oprot.writeBool(struct.success);
      }
      if (struct.isSetMsg()) {
        oprot.writeString(struct.msg);
      }
      if (struct.isSetUsers()) {
        {
          oprot.writeI32(struct.users.size());
          for (User _iter4 : struct.users)
          {
            _iter4.write(oprot);
          }
        }
      }
    }

    @Override
    public void read(org.apache.thrift.protocol.TProtocol prot, UserResponse struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
      java.util.BitSet incoming = iprot.readBitSet(3);
      if (incoming.get(0)) {
        struct.success = iprot.readBool();
        struct.setSuccessIsSet(true);
      }
      if (incoming.get(1)) {
        struct.msg = iprot.readString();
        struct.setMsgIsSet(true);
      }
      if (incoming.get(2)) {
        {
          org.apache.thrift.protocol.TList _list5 = iprot.readListBegin(org.apache.thrift.protocol.TType.STRUCT);
          struct.users = new java.util.ArrayList<User>(_list5.size);
          @org.apache.thrift.annotation.Nullable User _elem6;
          for (int _i7 = 0; _i7 < _list5.size; ++_i7)
          {
            _elem6 = new User();
            _elem6.read(iprot);
            struct.users.add(_elem6);
          }
        }
        struct.setUsersIsSet(true);
      }
    }
  }

  private static <S extends org.apache.thrift.scheme.IScheme> S scheme(org.apache.thrift.protocol.TProtocol proto) {
    return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme();
  }
}


thrfit的IDL也可以定义枚举,可以指定枚举对应的值,如下:

UserType.thrift

namespace java ltd.klein.thrift.server.api.param

enum UserType {
    CONSUMER,
    MERCHANT = 1
}


生成的Java的文件如下:

/**
 * Autogenerated by Thrift Compiler (0.21.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package ltd.klein.thrift.server.api.param;


@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.21.0)", date = "2024-11-22")
public enum UserType implements org.apache.thrift.TEnum {
  CONSUMER(0),
  MERCHANT(1);

  private final int value;

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

  /**
   * Get the integer value of this enum value, as defined in the Thrift IDL.
   */
  @Override
  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.
   */
  @org.apache.thrift.annotation.Nullable
  public static UserType findByValue(int value) { 
    switch (value) {
      case 0:
        return CONSUMER;
      case 1:
        return MERCHANT;
      default:
        return null;
    }
  }
}


4.Service

service对应的就是Java的接口,参数可以是基本类型也可以是结构体,同样需要标注顺序。

返回值引用的UserResponse.thrift的UserResponse struct,书写的格式如下:thrfit文件名.struct名

namespace java ltd.klein.thrift.server.api.iface

include '../resp/UserResponse.thrift'

service UserThriftServiceV2 {

    UserResponse.UserResponse findUser(1:i32 id);
}

实现类的需要实现UserThriftServiceV2.iface

package ltd.klein.thrift.server.service;

import ltd.klein.thrift.server.api.iface.UserThriftServiceV2;
import ltd.klein.thrift.server.api.resp.User;
import ltd.klein.thrift.server.api.resp.UserResponse;
import ltd.klein.thrift.server.builder.UserResponseBuilder;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Klein
 */
@Service
public class UserThriftServiceV2Impl implements UserThriftServiceV2.Iface {

   @Override
   public UserResponse findUser(int id) throws TException {
      if (id == 0) {
         User user = new User();
         user.setId(id);
         user.setNickName("zhangsan");
         List<User> users = List.of(user);
         return UserResponseBuilder.buildSuccess(users);
      }
      return UserResponseBuilder.buildSuccess(new ArrayList<>(0));
   }
}


到这里对于IDL文件的介绍就结束了,主要介绍了IDL的基本数据类型、容器、结构体与service的编写与用法。如有问题欢迎留言,共同进步。

相关推荐

听说你还不知道Java代码是怎么运行的?

作者:Jay_huaxiao前言作为一名Java程序员,我们需要知道Java代码是怎么运行的。最近复习了深入理解Java虚拟机这本书,做了一下笔记,希望对大家有帮助,如果有不正确的地方,欢迎提出,感激...

如何开始学习JAVA编程?

#记录我的9月生活#...

java后端开发需要学什么?

Java属于后端开发中最常见的语言之一,Java这种语言的体系比较中立,而且具备了构建多线程的能力,在许多大型互联网平台Java的应用范围特别广泛。  java后端主要涉及到如下4个技术:  第一、S...

细思极恐:你真的会写Java吗?

导语自2013年毕业后,今年已经是我工作的第4个年头了,总在做java相关的工作,终于有时间坐下来,写一篇关于java写法的一篇文章,来探讨一下如果你真的是一个java程序员,那你真的会写java吗?...

七年Java开发的一路辛酸史:分享面试京东、阿里、美团后的心得

前言我觉得有一个能够找一份大厂的offer的想法,这是很正常的,这并不是我们的饭后谈资而是每个技术人的追求。像阿里、腾讯、美团、字节跳动、京东等等的技术氛围与技术规范度还是要明显优于一些创业型公司...

我把Java基础编程及思维导图整理的超级详细,小白都能看懂

Java基础编程及其思维导图目录:Java学习导图一、Java基本语法1.关键字与标识符2.变量分类3.运算符4.流程控制二、数组1.数组概述2.一维数组3.二维数组4.数组常见算法5....

Java 开发中的 9 个实用技巧:案例详解

Java开发中,总有一些技巧能够帮助我们提高代码质量和开发效率。下面,我们来分享9个Java技巧,每个技巧都附上实际案例,帮助你在工作中立刻应用。1.合理使用final关键字...

Java 20年,以后将往哪儿走?

在今年的Java20周年的庆祝大会中,JavaOne2015的中心议题是“Java的20年”。甲骨文公司Java平台软件开发部的副总裁GeorgesSaab的主题演讲就将关注点放在了java...

推荐1个java快速开发项目,让你接私活不用愁

??大家好,我是小编南风吹,每天推荐一个小工具/源码,装满你的收藏夹,让你轻松节省开发效率,实现不加班不熬夜不掉头发!...

教你用Java开发一个简单的JVM

一、前言几年前,接到一个开发任务:用Java开发能运行Java智能合约的虚拟机。在开发Java智能合约时,只能使用智能合约SDK提供的类和一些Java常用类(8种基本数据类型包装类;String、Bi...

java实战教程(一)软件开发流程&amp;开发模式

这里小编为了方便处于不同学习阶段的童鞋,准备了三个系列的文章,java系列教程、java实战教程、java进阶教程,对于刚入坑的童鞋,可以先按照这三个系列教程一步步的了解,循序渐进,java实战系列教...

Java 核心技术之入门指南:全面解析Java概述

大家好,这里是Java码牛!Java核心技术入门:全面解析Java概述一、引言Java,作为一门在当今信息技术领域中被广泛应用于企业级开发的主流编程语言,其核心技术的精准掌握对于众多开发者而言,具有...

小白如何轻松上手Java开发?

Java,这款流行的编程语言,被广大开发者所钟爱。但对于初学者来说,如何入门确实是一个大问题。尤其对于毫无经验的小白,从何处开始、如何推进,都是关键。本文将带你走进Java的世界,为你揭示从零到一的进...

初学Java应该知道的知识点:Java的程序开发是什么?

Java的程序开发是什么呢?下面和千锋广州小编一起来看看吧!一般来说,Java的程序开发包括三个步骤:编写程序,编译程序,运行程序编写程序——Java源代码,.Java文件编译程序——Javac用来进...

厉害了!全靠经典之作-Java编程思想,把小白教的明明白白

今天我们来聊聊这本《Java编程思想》从我学习Java的经验来看,《ThinkinginJava》是讲解Java编程的最佳书籍!  这本书不仅详细地介绍Java语法、知识点、API类库使用,更...