Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- docker
- aws saa ํฉ๊ฒฉ
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- ํ๋ก๊ทธ๋๋จธ์ค
- ์๋ฐ
- Entity
- docker compose
- Codedeploy ์ค๋ฅ
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- redis ์กฐํ
- private subnet ec2 ๋ก์ปฌ ์ ์
- docker ps -a
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- Kafka
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- redis ํ ์คํธ์ฝ๋
- ์คํํ๋ ๋ฏธ์ค
- s3 log ์ ์ฅ
- JPA
- aws ์ฟ ํฐ
- AWS Certified Solutions Architect - Associate
- ๋ค์ค ์ปจํ ์ด๋
- prod docker-compose
- ํ์ดํผ๋ฐ์ด์
- nGrinder
- docker-compose kafka
Archives
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Spring] RestTemplate ๋ณธ๋ฌธ
๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/๐๐ฉ๐ซ๐ข๐ง๐
[Spring] RestTemplate
๐คRyusun๐ค 2023. 5. 19. 11:42RestTemplate๋?
- Spring์์ ์ง์ํ๋ ๊ฐ์ฒด๋ก ๊ฐํธํ๊ฒ Rest ๋ฐฉ์ API๋ฅผ ํธ์ถํ ์ ์๋ Spring ๋ด์ฅ ํด๋์ค
- Spring 3.0๋ถํฐ ์ง์๋์๊ณ , json, xml ์๋ต์ ๋ชจ๋ ๋ฐ์ ์ ์๋ค.
- Rest API ์๋น์ค๋ฅผ ์์ฒญ ํ ์๋ต ๋ฐ์ ์ ์๋๋ก ์ค๊ณ๋์ด์์ผ๋ฉฐ HTTP ํ๋กํ ์ฝ์ ๋ฉ์๋(ex. GET, POST, DELETE, PUT)๋ค์ ์ ํฉํ ์ฌ๋ฌ ๋ฉ์๋๋ค์ ์ ๊ณตํ๋ค.
RestTemplate์ ํน์ง
- Spring 3.0 ๋ถํฐ ์ง์ํ๋ Spring์ HTTP ํต์ ํ ํ๋ฆฟ
- HTTP ์์ฒญ ํ JSON, XML, String ๊ณผ ๊ฐ์ ์๋ต์ ๋ฐ์ ์ ์๋ ํ ํ๋ฆฟ
- Blocking I/O ๊ธฐ๋ฐ์ ๋๊ธฐ๋ฐฉ์์ ์ฌ์ฉํ๋ ํ ํ๋ฆฟ
- RESTful ํ์์ ๋ง์ถ์ด์ง ํ ํ๋ฆฟ
- Header, Content-Tpye๋ฑ์ ์ค์ ํ์ฌ ์ธ๋ถ API ํธ์ถ
- Server to Server ํต์ ์ ์ฌ์ฉ
API ํธ์ถ ํด๋์ค ์ข ๋ฅ
- RestTemplate
- Spring 3๋ถํฐ ์ง์ ๋์๊ณ REST API ํธ์ถ์ดํ ์๋ต์ ๋ฐ์ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ๋๊ธฐ๋ฐฉ์
- WebClient
- Spring 5์ ์ถ๊ฐ๋ ๋ ผ๋ธ๋ญ, ๋ฆฌ์ํฐ๋ธ ์น ํด๋ฆฌ์ด์ธํธ๋ก ๋๊ธฐ, ๋น๋๊ธฐ ๋ฐฉ์์ ์ง์ํ๋ค.
- RestTempalte ๊ณผ WebClient์ ์ฐจ์ด
- Non-Blocking๊ณผ ๋น๋๊ธฐํ ๊ฐ๋ฅ ์ฌ๋ถ
- ์คํ๋ง 4.0์์ RestTemplate์ ๋น๋๊ธฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด AsyncRestTemplate์ ์ ๊น ์ฌ์ฉํ๊ธด ํ์ง๋ง, ํ์ฌ deprecated ๋ ์ํ์ด๋ฏ๋ก WebClient์ ์ฌ์ฉ์ ๊ถ์ฅํ๋ค.
- Non-Blocking?
์์คํ ์ ํธ์ถํ ์งํ์ ํ๋ก๊ทธ๋จ์ผ๋ก ์ ์ด๊ฐ ๋ค์ ๋์์์ ์์คํ ํธ์ถ์ ์ข ๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค์ ๋์์ ์งํํ๋ค. ํธ์ถํ ์์คํ ์ ๋์์ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋์์ ๋ค๋ฅธ ์์ ์ ์งํํ ์ ์์ด์ ์์ ์ ์๋๊ฐ ๋นจ๋ผ์ง๋ค๋ ์ฅ์ ์ด ์๋ค.
์์กด์ฑ ์ค์
๊ธฐ๋ณธ ์คํ๋ง ๋ถํธ ์์กด์ฑ์ ์ถ๊ฐํ๋ฉด RestTemplate ๊ด๋ จ ์์กด์ฑ์ ์๋์ผ๋ก ์ถ๊ฐ๋๋ค
implementation 'org.springframework.boot:spring-boot-starter-web'
RestTemplate ๋ฉ์๋
์์ฒญ URI ์ค์ ๋ฐฉ๋ฒ
- String ๋ณ์ ์ฌ์ฉ, URI ๊ฐ์ฒด ์ฌ์ฉ, UriComponents ๊ฐ์ฒด ์ฌ์ฉ
package com.example.dockerphamarcyproject.api.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Slf4j
@Service
public class KakaoUriBuilderService {//uri๋ฅผ ๋ง๋๋ ์๋น์ค
private static final String KAKAO_LOCAL_SEARCH_ADDRESS_URL = "https://dapi.kakao.com/v2/local/search/address.json";
private static final String KAKAO_LOCAL_CATEGORY_SEARCH_URL = "https://dapi.kakao.com/v2/local/search/category.json";
public URI buildUriByAddressSearch(String address) {
//URI๋ฅผ ๊ตฌ์ฑํ๋ Components๋ค์ ํจ๊ณผ์ ์ผ๋ก ๋ค๋ฃฐ ์ ์๋๋ก ํ๋ ํด๋์ค
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(KAKAO_LOCAL_SEARCH_ADDRESS_URL);
uriBuilder.queryParam("query", address);
URI uri = uriBuilder.build().encode().toUri();
log.info("[KakaoAddressSearchService buildUriByAddressSearch] address: {}, uri: {}", address, uri);
return uri;
}
}
RestTemplate ์ฌ์ฉ ์์
- ๊ฒฐ๊ณผ๊ฐ์ ๋ด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- ํ์์์ ์ค์ ์ HttpComponentsClientHttpRequestFactory ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- RestTemplate ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- header ์ค์ ์ ์ํด HttpHeader ํด๋์ค๋ฅผ ์์ฑํ ํ HttpEntity ๊ฐ์ฒด์ ๋ฃ๋๋ค.
- ์์ฒญ URL์ ์ ์ํ๋ค.
- exchange() ๋ฉ์๋๋ก api๋ฅผ ํธ์ถํ๋ค.
- ์์ฒญํ ๊ฒฐ๊ณผ๋ฅผ HashMap์ ์ถ๊ฐํ๋ค.
package com.example.dockerphamarcyproject.api.service;
import com.example.dockerphamarcyproject.api.dto.KakaoApiResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
@Slf4j
@Service
@RequiredArgsConstructor
public class KakaoAddressSearchService {// ์ค์ ๋ก api๋ฅผ ํธ์ถํด์ ์๋ต๊ฐ์ ์ ๋ฐ์์ค๋์ง ํ์ธ
private final RestTemplate restTemplate; // ์์กด์ฑ ์ฃผ์
private final KakaoUriBuilderService kakaoUriBuilderService;
@Value("${kakao.rest.api.key}")
private String kakaoRestApiKey;
@Retryable(
value = {RuntimeException.class},
maxAttempts = 2,
backoff = @Backoff(delay = 2000)
)
public KakaoApiResponseDto requestAddressSearch(String address) {
//๊ณ ๊ฐ์ด ๋ฌธ์ ๊ธฐ๋ฐ ์ฃผ์๋ก ์์ฒญํ๊ฒ ๋๋ฉด ์ด ๋ฉ์๋๊ฐ ์์น ๊ธฐ๋ฐ ๋ฐ์ดํฐ(KakaoApiResponseDto)๋ก ๋ฐํํจ
if(ObjectUtils.isEmpty(address)) return null;
URI uri = kakaoUriBuilderService.buildUriByAddressSearch(address); //์ค์ ์นด์นด์ค uri๋ฅผ ๋ฐํํ๋ค.
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "KakaoAK " + kakaoRestApiKey);
// HttpEntity = HTTP์์ฒญ๋๋ ์๋ต์ ํด๋นํ๋ HttpHeader์ HttpBody๋ฅผ ํฌํจํ๋ ํด๋์ค
// HttpEntity ํด๋์ค๋ฅผ ์์๋ฐ์ ๊ตฌํํ ํด๋์ค๊ฐ RequestEntity, ResponseEntity ํด๋์ค์ด๋ค.
HttpEntity httpEntity = new HttpEntity<>(headers);
// restTemplate.exchange(): ์ง์ ๋ HTTP ๋ฉ์๋๋ฅผ URL์ ๋ํด ์คํํ๋ฉฐ, Responsebody์ ์ฐ๊ฒฐ๋๋ ๊ฐ์ฒด๋ฅผ ํฌํจํ responseEntity๋ฅผ ๋ฐํํ๋ค.
return restTemplate.exchange(uri, HttpMethod.GET, httpEntity, KakaoApiResponseDto.class).getBody();//body๋ง ํ์ํด์ body return
// exchange(uri, ๋ฉ์๋ ๋ฐฉ์, httpEntity, ์ ๋ฌ๋ฐ์ ํด๋์ค)
}
//์ฌ์ฒ๋ฆฌ๋ฅผ ๋ชจ๋ ๋ค ์คํจํ์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ๋ฐฉ๋ฒ ์ ์
//์ฃผ์ํด์ผํ ์ ์ ์๋ ๋ฉ์๋์ return ํ์
์ ๋ง์ถฐ์ค์ผํ๋ค.
@Recover
public KakaoApiResponseDto recover(RuntimeException e, String address){
log.error("All the retries failed. address : {}, error : {}", address, e.getMessage());
return null;
}
}
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐ฉ๐ซ๐ข๐ง๐ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Batch] Tasklet, Chunk (0) | 2023.06.22 |
---|---|
[Spring] Batch (0) | 2023.06.21 |
[Spring] MockWebServer (0) | 2023.05.17 |
[Spring] Spring Retry (1) | 2023.05.16 |
[Spring] Spock (0) | 2023.05.15 |