[Spring] RestTemplate

์ง€์ธยท2023๋…„ 6์›” 26์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
12/20
post-custom-banner

๐Ÿฐ RestTemplate

Spring์—์„œ ์ง€์›ํ•˜๋Š” ๊ฐ์ฒด๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ Rest ๋ฐฉ์‹ API๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” Spring ๋‚ด์žฅ ํด๋ž˜์Šค์ด๋‹ค.

๐Ÿพ 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 ํ†ต์‹ ์— ์‚ฌ์šฉ


RestTemplate ๋™์ž‘ ์›๋ฆฌ

  1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด๋ถ€์—์„œ REST API์— ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด RestTemplate์˜ ๋ฉ”์…”๋“œ๋ฆด ํ˜ธ์ถœ ํ•œ๋‹ค.

  2. RestTemplate์€ MessageConverter๋ฅผ ์ด์šฉํ•ด java object๋ฅผ request body์— ๋‹ด์„ message(JSON etc.)๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ฉ”์‹œ์ง€ ํ˜•ํƒœ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.

  3. ClientHttpRequestFactory์—์„œ ClientHttpRequest์„ ๋ฐ›์•„์™€ ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค.

  4. ์‹ค์งˆ์ ์œผ๋กœ ClientHttpRequest๊ฐ€ HTTP ํ†ต์‹ ์œผ๋กœ ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  5. RestTemplate์ด ์—๋Ÿฌํ•ธ๋“ค๋ง์„ ํ•œ๋‹ค.

  6. ClientHttpResponse์—์„œ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ์ฒ˜๋ฆฌํ•œ๋‹ค.

  7. MessageConverter๋ฅผ ์ด์šฉํ•ด response body์˜ message๋ฅผ java object๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

  8. ๊ฒฐ๊ณผ๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ๋ ค์ค€๋‹ค.


์ง€์› ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œHTTP์„ค๋ช…
getForObject : ObjectGETGET ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜
getForEntity : ResponseEntityGETGET ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ResponseEntity๋กœ ๋ฐ˜ํ™˜
postForLocationPOSTPOST ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ํ—ค๋”์— ์ €์žฅ๋œ URI๋กœ ๋ฐ˜ํ™˜
postForObject : ObjectPOSTPOST ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜
postForEntity : ResponseEntityPOSTPOST ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ResponseEntity๋กœ ๋ฐ˜ํ™˜
deleteDELETEDELETE ๋ฐฉ์‹ ์š”์ฒญ์œผ๋กœ ๋ฉ”์„œ๋“œ ์‹คํ–‰
headForHeadersHEADERํ—ค๋”์˜ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ณ , HTTP HEAD ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
putPUTHTTP PUT ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰
patchForObjectPATCHHTTP PATCH ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰
optionsForAllowOPTIONS์ง€์›ํ•˜๋Š” HTTP ๋ฉ”์„œ๋“œ ์กฐํšŒ
exchange : ResponseEntityanyํ—ค๋” ์ƒ์„ฑ ๋ฐ ์–ด๋–ค ์š”์ฒญ์ด๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅ
executeanyRequest/Response ์ฝœ๋ฐฑ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ

RestTemplate ์‚ฌ์šฉ

1) ์˜์กด์„ฑ ์ถ”๊ฐ€

implementation 'org.apache.httpcomponents:httpcore:4.4.15'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'

2) ์‚ฌ์šฉ์˜ˆ์ œ

@PostMapping(path = "/getInfo")
public String getInfo() {
   // request url
   String url = "";
   String jsonData = "{}";

   // create an instance of RestTemplate
   RestTemplate restTemplate = new RestTemplate();
   HttpHeaders headers = new HttpHeaders();
   MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
   headers.setContentType(mediaType);
   HttpEntity<String> entity = new HttpEntity<>(jsonData, headers);

   String response = restTemplate.postForObject(url, entity, String.class);
   System.out.println(response);

   return response;
}

์ฐธ๊ณ 

๋กœ๊ทธ์˜ ๊ฐœ๋ฐœ์ผ์ง€

profile
์—ด์ฉก
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€