Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
retrofit [2017/06/16 18:10] ledyxretrofit [2021/02/07 05:49] (current) – old revision restored (2017/06/22 14:06) ledyx
Line 50: Line 50:
         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 = (Stringf.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();
Line 72: Line 80:
                     .client(client)                     .client(client)
                     .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                     .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
-                    .addConverterFactory(JacksonConverterFactory.create())+                    .addConverterFactory(getConverterFactory(isUnwrapRootValue))
                     .build().create(restfulInterface);                     .build().create(restfulInterface);
  
Line 81: Line 89:
     }     }
  
 +    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);
 +    }
 +    
     /**     /**
      * 인증서 무시      * 인증서 무시
Line 107: Line 126:
  
 <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);
Line 123: Line 140:
     @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>
retrofit.1497633000.txt.gz · Last modified: 2021/02/07 03:15 (external edit)