- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
HttpClient
        readeyKim edited this page Aug 14, 2019 
        ·
        1 revision
      
    HCL을 사용하기 위해서는 먼저 HCL에서 제공하고 있는 HttpClient 객체를 생성해야 합니다.
HttpClient 객체를 통해서 baseUrl, timeout, interceptor 등에 대한 설정과 Service Interface에 대한 구현을 할 수 있습니다.
객체 생성은 다음과 같이 HttpClient 내부에 정의되어 있는 Builder를 통해서 생성합니다.
HttpClient httpClient = new HttpClient.Builder()
                        .baseUrl("http://jsonplaceholder.typicode.com/") // 생략 가능
                        .build();HttpClient 객체는 우리가 제시하는 룰을 따른 인터페이스에 한해 통신을 위한 구현 객체로 재구성하여 제공합니다.
Service Interface가 지켜야할 룰은 다음과 같습니다.
- class가 아닌 interface로 정의합니다.
- 메소드의 리턴 타입은 반드시 CallTask<> 이어야 합니다.
- 각 메소드는 @RequestMapping이나 @DynamicURL 둘 중 하나를 반드시 동반합니다.
- 중복은 허용하지 않습니다.
- @RequestMapping을 사용하는 경우는 relative URL 값과 http method를 지정합니다.
- default - value() : "/", method() : RequestMethod.GET
 
- @DynamicURL의 경우 http method만 지정합니다.
- URL은 파라미터로 입력 받아야 하며, 이 경우 HttpClient 객체가 가지고 있는 baseUrl과는 무관합니다.
 
 
Service Interface 생성 예시는 다음과 같습니다.
public interface ServiceExample {
    // baseUrl : "http://jsonplaceholder.typicode.com"
    @RequestMapping(value="/posts", method=RequestMethod.GET)
    CallTask<List<Post>> getAllPosts();
    // parameter로 "http://jsonplaceholder.typicode.com/posts" 전달시 위 메소드와 같은 결과
    @DynamicURL(method=RequestMethod.GET)
    CallTask<List<Post>> getAllPosts(@URL String url);
}HttpClient 객체는 Service Interface의 class 정보를 통해 인스턴스를 제공합니다. 예시는 다음과 같습니다.
ServiceExample service = httpClient.create(ServiceExample.class);