retrofit-plus

Introduction: retrofit-plus 是一款基于 retrofit2 实现的轻量级 httpClient 客户端工具,与 spring 和 spring-boot 项目深度集成。
More: Author   ReportBugs   
Tags:

License Maven Central License License Ask DeepWiki

English | 简体中文 | 繁體中文 | 日本語 | 한국어 | Español | Türkçe | Русский

Retrofit 支持将 HTTP API 化成 Java 接口,本组件将 Retrofit 和 Spring Boot 深度整合,并支持了多种实用功能增强。

  • Spring Boot 3.x/4.x 项目,请使用 retrofit-spring-boot-starter 4.x
    • 由于 Spring Boot 4.x 默认使用 Jackson 3,而本组件默认 Converter 使用 Jackson 2,因此 4.x 项目建议将全局 Converter 设置为 Jackson 3
    • 配置方式:retrofit.global-converter-factories=com.github.lianjiatech.retrofit.spring.boot.core.jackson3.Jackson3ConverterFactory
  • Spring Boot 1.x/2.x 项目,请使用 retrofit-spring-boot-starter 2.x,支持 Spring Boot 1.4.2 及以上版本

项目持续优化迭代,欢迎大家提 ISSUE 和 PR!能给一颗 star,是对我们持续更新的最大支持!

GitHub:https://github.com/LianjiaTech/retrofit-spring-boot-starter Gitee:https://gitee.com/lianjiatech/retrofit-spring-boot-starter

快速开始

引入依赖

<dependency>
    <groupId>com.github.lianjiatech</groupId>
    <artifactId>retrofit-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>

引入依赖后即可使用。如遇问题,参见常见问题

定义 HTTP 接口

接口必须使用 @RetrofitClient 注解标记!

@RetrofitClient(baseUrl = "http://localhost:8080/api/user/")
public interface UserService {

    /**
     * 根据 id 查询用户姓名
     */
    @POST("getName")
    String getName(@Query("id") Long id);
}

注意:方法请求路径慎用 / 开头。Retrofit 的路径拼接规则:如果 baseUrl = http://localhost:8080/api/test/,方法路径 person 的完整路径为 http://localhost:8080/api/test/person;而方法路径 /person 的完整路径为 http://localhost:8080/person

注入使用

将接口注入到其他 Service 中即可使用:

@Service
public class BusinessService {

    @Autowired
    private UserService userService;

    public void doBusiness() {
        // 调用 userService
    }
}

默认情况下,自动使用 SpringBoot 扫描路径进行 RetrofitClient 注册,也可以在配置类加上 @RetrofitScan 手动指定扫描路径。

HTTP 请求注解

HTTP 请求相关注解全部使用 Retrofit 原生注解:

注解分类 支持的注解
请求方式 @GET @HEAD @POST @PUT @DELETE @OPTIONS @HTTP
请求头 @Header @HeaderMap @Headers
Query 参数 @Query @QueryMap @QueryName
Path 参数 @Path
Form 参数 @Field @FieldMap @FormUrlEncoded
请求体 @Body
文件上传 @Multipart @Part @PartMap
URL 参数 @Url

详细信息参考 Retrofit 官方文档

功能特性

反馈建议

如有任何问题,欢迎提 issue 或者加 QQ 群反馈。

群号:806714302

QQ 群图片

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools
AI Daily Digest