Both sides previous revisionPrevious revisionNext revision | Previous revision |
retrofit [2017/06/16 18:10] – ledyx | retrofit [2021/02/07 05:49] (current) – old revision restored (2017/06/22 14:06) ledyx |
---|
T service = (T) services.get(restfulInterface.getName()); | T service = (T) services.get(restfulInterface.getName()); |
| |
if(service == null) { | if(service == null) { |
Field f = restfulInterface.getField("URL"); | if(!restfulInterface.isAnnotationPresent(URL.class)) |
String url = (String) f.get(null); | throw new Exception("Must be annotated URL"); |
| |
| URL urlAnnotation = restfulInterface.getDeclaredAnnotation(URL.class); |
| |
| String url = urlAnnotation.value(); |
| if(url.length() <= 0 || !url.substring(0, 4).equals("http")) |
| throw new Exception("Unavailable URL value"); |
| |
| boolean isUnwrapRootValue = urlAnnotation.isUnwrappedRoot(); |
| |
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); | HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); |
.client(client) | .client(client) |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
.addConverterFactory(JacksonConverterFactory.create()) | .addConverterFactory(getConverterFactory(isUnwrapRootValue)) |
.build().create(restfulInterface); | .build().create(restfulInterface); |
| |
} | } |
| |
| private static JacksonConverterFactory getConverterFactory(boolean isUnwrapRootValue) { |
| ObjectMapper objectMapper = new ObjectMapper(); |
| objectMapper.registerModule(new JodaModule()); |
| objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); |
| objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| if(isUnwrapRootValue) |
| objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); // JsonRootName과 연계 |
| |
| return JacksonConverterFactory.create(objectMapper); |
| } |
| |
/** | /** |
* 인증서 무시 | * 인증서 무시 |
| |
<sxh java ; title:Service Interface> | <sxh java ; title:Service Interface> |
| @URL(value = "http://xxx.com", isUnwrappedRoot = true) |
public interface DemoService { | public interface DemoService { |
| |
String URL = "http://111.111.111.111/"; | |
| |
@GET("{page}") | @GET("{page}") |
Observable<ContentsVO> read(@Path("page") long page); | Observable<ContentsVO> read(@Path("page") long page); |
@DELETE("{no}") | @DELETE("{no}") |
Observable<ResponseBody> delete(@Path("no") long no); | Observable<ResponseBody> delete(@Path("no") long no); |
| } |
| </sxh> |
| |
| <sxh java ; title:Custom Annotation> |
| @Target(ElementType.TYPE) |
| @Retention(RetentionPolicy.RUNTIME) |
| public @interface URL { |
| String value() default ""; |
| boolean isUnwrappedRoot() default false; |
} | } |
</sxh> | </sxh> |