This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| retrofit [2017/05/30 02:49] – 만듦 ledyx | retrofit [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: |
| 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) { |
| - | | + | |
| - | String url = (String) f.get(null); | + | throw new Exception("Must be annotated |
| + | |||
| + | URL urlAnnotation = restfulInterface.getDeclaredAnnotation(URL.class); | ||
| + | |||
| + | String url = urlAnnotation.value(); | ||
| + | if(url.length() <= 0 || !url.substring(0, | ||
| + | throw new Exception(" | ||
| + | |||
| + | 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); | ||
| + | | ||
| + | return JacksonConverterFactory.create(objectMapper); | ||
| + | } | ||
| + | | ||
| /** | /** | ||
| * 인증서 무시 | * 인증서 무시 | ||
| Line 103: | Line 122: | ||
| return builder; | return builder; | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: | ||
| + | @URL(value = " | ||
| + | public interface DemoService { | ||
| + | @GET(" | ||
| + | Observable< | ||
| + | |||
| + | @PUT(" | ||
| + | Observable< | ||
| + | |||
| + | @Multipart | ||
| + | @POST(URL) | ||
| + | Observable< | ||
| + | |||
| + | @DELETE(" | ||
| + | Observable< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: | ||
| + | @Target(ElementType.TYPE) | ||
| + | @Retention(RetentionPolicy.RUNTIME) | ||
| + | public @interface URL { | ||
| + | String value() default ""; | ||
| + | boolean isUnwrappedRoot() default false; | ||
| } | } | ||
| </ | </ | ||