# retrofitor **Repository Path**: freedom_pursuit/retrofitor ## Basic Information - **Project Name**: retrofitor - **Description**: 对Retrofit进行封装,便于在spring环境下快速开发 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2016-04-21 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #Retrofitor ### What is Retrofitor Retrofitor is a better encapsulation of [Retrofit ](https://github.com/square/retrofit) when using spring framework to develop webapp. ### How to get Maven ``` net.oschina.bilbodai.common.retrofitor retrofitor 1.0.0 ``` ### How to Use Before using Retrofitor, I recommend you first go over the document of [Retrofit Doc](http://square.github.io/retrofit/) . First declare your Http Service Interface ``` @HttpService(baseUrl = "http://localhost:8080") public interface RetrofitorService { @GET("/foo") Call foo(); } ``` Add spring configuration ``` ``` *make sure `` include `RetrofitorService` above for Retrofitor to scan*. Inject and you are good to go ``` @Resource private RetrofitorService retrofitorService; @Test public void testIndex() throws Exception { Foo foo = retrofitorService.foo().execute().body(); Assert.assertEquals("foo",foo.getName()); } ``` ### Custom Configuration ``` public @interface HttpService { /** * 这个httpservice对应的根URL,如:https://api.github.com/ * 注意,查找逻辑优先级 baseUrl > IURLProvider > IGlobalURLProvider,如果都未找到,则创建过程报错 */ String baseUrl() default ""; /** * 采用单独的一个逻辑为这个Service提供BaseURl */ Class urlProvider() default IURLProvider.class; /** * 如果要为这个服务单独指定httpClient,则通过解析时会通过这个字段从容器中获取 */ String httpClientRef() default ""; /** * 如果为true,则当httpClientRef为空的时候,会先去上下文获取全局的HttpClient,如果未找到,则抛出异常 * 如果想为这个服务单独初始化一个默认的HttpClient,则把这个设置为false.并且不要设置httpClientRef */ boolean httpClientAutoInject() default true; /** * 从容器中查找相关的converter,如果为空,则默认添加Jackson */ String[] converterFactories() default {}; /** * 从容器中查找相关的adapter */ String[] adapterFactories() default {}; /** * 如果为true,则默认会加入jackson,xml的converter */ boolean useDefaultConverters() default true; } ``` Additionally Retrofitor provided DefaultHttpClient Configraution Factory Bean ``` ``` ## 版本更新 ## V1.0.2 Adding Two Flags to control auto injection of converters and adapters ``` /** * 如果为true,则会自动搜索spring上下文中的{@link retrofit2.Converter.Factory}并注入到实例中 * @return 是否自动注入 */ boolean convertersAutoInject() default true; /** * 如果为true,则会自动搜索spring上下文中的{@link retrofit2.CallAdapter.Factory}并注入到实例中 * @return 是否自动注入 */ boolean adapterAutoInject() default true; ``` Declare in spring context ``` ```