Skip to content
Projets

LianjiaTech/retrofit-spring-boot-starter

Voir sur GitHub

À propos

Un starter Spring Boot pour Retrofit, qui permet une intégration rapide et des améliorations fonctionnelles.

Développement web et applicatif

Comment ces chiffres sont mesurés

Progression mesurée des étoiles

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
-1

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original

retrofit-spring-boot-starter

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.1</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群图片

Explorer des projets similaires

Chargement…