目录

1、Thrift注解开发介绍

2、注解Demo

1、服务接口

2、各个类

3、枚举

3、注解和IDL规范

4、编码规范


​​​​​​​

1、Thrift注解开发介绍

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

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

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

2、注解Demo

1、服务接口

@ThriftService
public interface TestService {@ThriftMethodpublic TestResponse method1(TestRequest testRequest) throws TException;@ThriftMethodpublic 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;@ThriftConstructorpublic 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;}@ThriftFieldpublic void setUserId(Integer userId) {this.userId = userId;}@ThriftField(2)public String getMessage() {return message;}@ThriftFieldpublic void setMessage(String message) {this.message = message;}@ThriftField(value = 3,requiredness = ThriftField.Requiredness.OPTIONAL)public Integer getSeqId() {return seqId;}@ThriftFieldpublic void setSeqId(Integer seqId) {this.seqId = seqId;}@Overridepublic 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);}@Overridepublic int hashCode() {return Objects.hash(userId, message, seqId);}@Overridepublic String toString() {return "TestResponse{" +"userId=" + userId +", message='" + message + '\'' +", seqId=" + seqId +'}';}
}
@ThriftStruct
public class TestResponse {private Integer userId;private String message;private Integer seqId;@ThriftConstructorpublic 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;}@ThriftFieldpublic void setUserId(Integer userId) {this.userId = userId;}@ThriftField(2)public String getMessage() {return message;}@ThriftFieldpublic void setMessage(String message) {this.message = message;}@ThriftField(value = 3,requiredness = ThriftField.Requiredness.OPTIONAL)public Integer getSeqId() {return seqId;}@ThriftFieldpublic void setSeqId(Integer seqId) {this.seqId = seqId;}@Overridepublic 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);}@Overridepublic int hashCode() {return Objects.hash(userId, message, seqId);}@Overridepublic String toString() {return "TestResponse{" +"userId=" + userId +", message='" + message + '\'' +", seqId=" + seqId +'}';}
}
@ThriftStruct
public class TestException extends AbstractThriftException {private String message;@ThriftConstructorpublic TestException(String message) {this.message = message;}@Override@ThriftField(1)public String getMessage() {return message;}@ThriftFieldpublic 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;}@ThriftEnumValuepublic 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注解

Thrift(二):Thrift注解相关推荐

  1. Json转换利器Gson之实例二-Gson注解和GsonBuilder

    有时候我们不需要把实体的所有属性都导出,只想把一部分属性导出为Json. 有时候我们的实体类会随着版本的升级而修改. 有时候我们想对输出的json默认排好格式. ... ... 请看下面的例子吧: 实 ...

  2. spring入门(二) 使用注解代替xml配置

    1.导包(略) 2.applicationContext.xml如下: 1 <?xml version="1.0" encoding="UTF-8"?&g ...

  3. Thrift架构~thrift中间语言的认识(只有它什么都不是,它才有可能什么都是)

    中间语言是我为thrift语言起的名字,呵呵,为什么叫它中间语言呢,其实意思很简单,就是说,它只是一个过程,而不是一个最终的结果,而结果需要这个中间过程的影响,没有中间语言,结果语言也不能产生,在th ...

  4. 深入学习Spring框架(二)- 注解配置

    1.为什么要学习Spring的注解配置? 基于注解配置的方式也已经逐渐代替xml.所以我们必须要掌握使用注解的方式配置Spring. 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 ...

  5. 【Thrift】Thrift框架和使用教程-- C/S框架使用教程

    1.前言 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC的接口和 ...

  6. SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis

    前面已经创建数据表和JAVABEAN,如何用mybatis来对数据进行增删改查,我们先说mybatis注解版的使用,我来写上一个mapper,操作我们这个数据库,我们放在mapper包下,我们操作de ...

  7. spring boot注解_Spring-boot(二)注解

    首先从启动处注解: @SpringBootApplication 查看源码可知为复合注解: SpringBootConfiguration:这个是spring-boot定义的注解 @Target({E ...

  8. SpringMVC学习记录二——非注解和注解的处理器映射器和适配器

    3      非注解的处理器映射器和适配器 3.1      非注解的处理器映射器 处理器映射器: org.springframework.web.servlet.handler.BeanNameUr ...

  9. mybatis应用(二)注解的实现

    开门见山吧!想了解mybatis的朋友可以去上一篇看看,另外mybatis开发中需要用到的jar包,我都已经上传了,需要的可以直接下载 [mybatis] mybatis-3.1.1.jar  下载地 ...

  10. 跨语言RPC框架Thrift详解

    一. 概念 Apache的Thrift软件框架,是用来进行可伸缩的.跨语言的服务开发,它通过一个代码生成引擎来构建高效.无缝的服务,这些服务能够实现跨语言调度,目前支持的语言有: C++, Java, ...

最新文章

  1. C++Builder及VC的库相互调用
  2. 正则表达式中问号等特殊字符的转义(转)
  3. 进入公司前与Boss的会谈话
  4. 推荐10款非常有用的 Ajax 插件
  5. Install ruby on rails in Ubuntu 12.04
  6. 推翻自己和过往,重学自定义View
  7. 如何形成统一设计风格-实践篇
  8. 针对递归函数的优化与Python修饰器实现
  9. python split()方法_聊聊 Python 的单元测试框架(一):unittest
  10. cv::Mat ptr 和 at 注意事项
  11. c语言小游戏如何编写,如何用c语言编写小游戏.docx
  12. 论文写作中插入公式间距变大怎么办?
  13. echarts实现立体柱状图
  14. python3网络爬虫开发实战6.3爬取微博处理since_id
  15. 验证OpenVino时,找不到指定模块的解决
  16. 学计算机应用的必懂知识,学习计算机应用基础心得体会
  17. Gradle-SpringCloud聚合项目配置
  18. 爬取获取邮箱内容QQ邮箱or163邮箱
  19. 神经复杂系统前沿:关于认知大脑的两种观念
  20. GEE:LST地表温度反演函数(针对Landsat8的T1影像和T1_L2影像集合)

热门文章

  1. HDS NAS原理及Storage Pool方案
  2. Java基于JSP+Servlet的校友论坛管理系统
  3. 易思ESPCMS企业建站管理系统 P8.21120101 稳定版
  4. 图中最深的根 (25分)
  5. spring security中A granted authority textual representation is required
  6. FlashFXP 便携版,U盘版,真正绿色
  7. java docx4j 合并word_如何使用docx4j在word中添加合并字段?
  8. [JZOJ3337] 【NOI2013模拟】wyl8899的TLE
  9. 关于视频编码I P B帧存储,编码,解码,显示顺序的理解
  10. 跑马灯实现的三种方式