ex)
1. new URL("http:// ...")
2. openConnection()
3. URLConnection
4. getInputStream, getOutputStream
5. InputStream, OutputStream 처리
ex)
1. CloseableHttpClient httpclient = HttpClients.createDefault();
2. 메소드에 따라 new HttpGet("http:// ...");
3. CloseableHttpResponse response = httpclient.execute(httpget);
4. HttpEntity entity = response.getEntity();
5. Stream 으로 entity.getContent() 처리 등
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(5000); // 읽기 최대 시간 지정, ms
factory.setConnectTimeout(3000); // 연결 최대 시간 지정, ms
HttpClient httpClient = httpClientBuilder.create()
.setMaxConnTotal(100) // connection pool 적용
.setMaxConnPerRoute(5) // connection pool 적용
.build();
factory.setHttpClient(httpClient); // 동기 실행에 사용될 HttpClient 세팋ㅇ
RestTemplate restTemplate = new RestTemplate(factory);
public void getForObject(){
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api")
.queryParam("name", "shbae")
.queryParam("age", "29")
.encode()
.build()
.toUri();
log.info("uri: {}", uri);
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject(uri, User.class);
log.info("user: {}", user);
}
public void getForEntity(){
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/{path}")
.queryParam("name", "shbae")
.queryParam("age", 29)
.encode()
.build()
.expand("user")
.toUri();
log.info("uri: {}", uri);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<User> response = restTemplate.getForEntity(uri, User.class);
log.info("{}", response.getStatusCode());
log.info("{}", response.getHeaders());
log.info("{}", response.getBody());
}
public void postForObject(){
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/{path}")
.encode()
.build()
.expand("user")
.toUri();
log.info("uri: {}", uri);
RestTemplate restTemplate = new RestTemplate();
User user = new User();
user.setName("shbae");
user.setAge(29);
User response = restTemplate.postForObject(uri, user, User.class);
log.info("response: {}", response);
}
public void postForEntity(){
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/{path}")
.encode()
.build()
.expand("user")
.toUri();
log.info("uri: {}", uri);
User user = new User();
user.setName("shbae");
user.setAge(29);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<User> response = restTemplate.postForEntity(uri, user, User.class);
log.info("{}", response.getStatusCode());
log.info("{}", response.getHeaders());
log.info("{}", response.getBody());
}
public void exchange(){
URI uri = UriComponentsBuilder
.fromUriString("http://localhost:9090")
.path("/api/{path}/header")
.encode()
.build()
.expand("user")
.toUri();
log.info("uri: {}", uri);
User user = new User();
user.setName("shbae");
user.setAge(29);
RequestEntity<User> req = RequestEntity
.post(uri)
.contentType(MediaType.APPLICATION_JSON)
.header("x-authorization", "my-header")
.body(user);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<User> response = restTemplate.exchange(req, new ParameterizedTypeReference<User>() {});
log.info("{}", response.getStatusCode());
log.info("{}", response.getHeaders());
log.info("{}", response.getBody());
}