Differences

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

Link to this comparison view

Next revision
Previous revision
retrofit [2017/05/30 03:49] – 만듦 ledyxretrofit [2021/02/07 05:49] (current) – old revision restored (2017/06/22 14:06) ledyx
Line 35: Line 35:
 = Source = = Source =
  
-<sxh java>+<sxh java ; title:Adapter>
 public class RestfulAdapter { public class RestfulAdapter {
  
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 103: Line 122:
         return builder;         return builder;
     }     }
 +}
 +</sxh>
 +
 +<sxh java ; title:Service Interface>
 +@URL(value = "http://xxx.com", isUnwrappedRoot = true)
 +public interface DemoService {
 +    @GET("{page}")
 +    Observable<ContentsVO> read(@Path("page") long page);
 +
 +    @PUT("{no}")
 +    Observable<ResponseBody> modify(@Path("no") long no, @Body ServiceVO serviceVO);
 +
 +    @Multipart
 +    @POST(URL)
 +    Observable<ResponseBody> write(@PartMap Map<String, RequestBody> params);
 +
 +    @DELETE("{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.1496112597.txt.gz · Last modified: 2021/02/07 03:15 (external edit)