Spring boot
Bank App 만들기 - Server To Server 개념
햄발자
2024. 9. 28. 00:06
학습 목표
- RestTemplate 사용 이유
RestTemplate은 Spring Framework에서 제공하는 HTTP 통신을 간편하게 처리할 수 있는 클래스입니다. org.springframework.web.client.RestTemplate 패키지에 존재 합니다. RESTful 웹 서비스와의 통신을 위해 주로 사용되고 기본적으로 동기 방식으로 처리되며, 비동기 방식으로 처리하고 싶을 경우 AsyncRestTemplate를 사용하면 됩니다.
https://jsonplaceholder.typicode.com/
JSONPlaceholder - Free Fake REST API
{JSON} Placeholder Free fake and reliable API for testing and prototyping. Powered by JSON Server + LowDB. Serving ~3 billion requests each month.
jsonplaceholder.typicode.com
https://jsonplaceholder.typicode.com/
RestTemplate 대표적 메서드
RestTemplate Method HTTP Method 설명
getForEntity | GET | get 요청을 보내고 ResponseEntity로 응답을 받음 |
getForObject | GET | get 요청을 보내고 java object로 매핑받아서 반환받음 |
exchange | Any | 헤더 세팅해서 HTTP Method로 요청보내고 ResponseEntity로 반환받음 |
put | PUT | PUT 형식으로 요청 |
delete | DELETE | DELETE 형식으로 요청 |
postForLocation | POST | post 요청을 보내고 java.net.URI 로 반환받음 |
postForObject | POST | post 요청을 보내고 Object로 반환받음 |
postForEntity | POST | POST 방식으로 요청하면 ResponseEntity를 반환해 준다. |
optionsForAllow | OPTIONS | 해당 URI에서 지원하는 HTTP 메서드를 조회 |
execute | Any | 요청과 응답에 대한 콜백 수정 |



http://localhost:8080/m-todos/10
HomeController.java
더보기
닫기
HomeController.java
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;
import com.tenco.bank.dto.Todo;
// @Controller + @ ResponseBody = RestController // IOC 대상
@RestController
public class HomeController {
/**
* 주소 설계 : http://localhost:8080/m-todos/${id}
* 테스트 주소 : http://localhost:8080/m-todos/10
* @param param
* @return
*/
@GetMapping("/m-todos/{id}")
public ResponseEntity<?> restTest1(@PathVariable(name = "id") Integer id) {
// 아직 정하지 않았다면 ? 넣어도 된다.
// 1. 데이터 추출 확인
System.out.println("id : " + id);
// RestTemplate 사용법
// 1. URL 객체를 설정한다.
URI uri = UriComponentsBuilder
.fromUriString("https://jsonplaceholder.typicode.com/")
.path("/todos")
.path("/" + id)
.build()
.toUri();
// 2.
RestTemplate restTemplate1 = new RestTemplate();
ResponseEntity<String> response = restTemplate1.getForEntity(uri, String.class); // 동기적 방식
System.out.println(response.getStatusCodeValue());
System.out.println("-------------------");
System.out.println(response.getHeaders());
System.out.println("-------------------");
System.out.println(response.getBody());
// 1, 2, User --> 와일드 카드
// return ResponseEntity.status(HttpStatus.OK).body("반가워");
return ResponseEntity.status(HttpStatus.OK).body(response.getBody());
} // end of restTest1()
}
HomeController.java
더보기
닫기
HomeController.java
/**
* 주소 설계 : http://localhost:8080/m-todos/${id}
* 테스트 주소 : http://localhost:8080/m-todos/10
* @param param
* @return
*/
@GetMapping("/m-todos/{id}")
public ResponseEntity<?> restTest1(@PathVariable(name = "id") Integer id) {
// 아직 정하지 않았다면 ? 넣어도 된다.
// 1. 데이터 추출 확인
System.out.println("id : " + id);
// RestTemplate 사용법
// 1. URL 객체를 설정한다.
URI uri = UriComponentsBuilder
.fromUriString("https://jsonplaceholder.typicode.com/")
.path("/todos")
.path("/" + id)
.build()
.toUri();
// 2.
RestTemplate restTemplate1 = new RestTemplate();
ResponseEntity<Todo> response = restTemplate1.getForEntity(uri, Todo.class); // 동기적 방식
// 1, 2, User --> 와일드 카드
// return ResponseEntity.status(HttpStatus.OK).body("반가워");
return ResponseEntity.status(HttpStatus.OK).body(response.getBody());
} // end of restTest1()
/**
* 주소설계 : http://localhost:8080/exchange-test
* @param param
* @return
*/
@GetMapping("/exchange-test")
public ResponseEntity<?> getMethodName() {
// 여기 주소는 리소스 서버 주소 설정을 해야 한다.
URI uri = UriComponentsBuilder
.fromUriString("https://jsonplaceholder.typicode.com/")
.path("/posts")
.build()
.toUri();
// 2. 객체 생성
RestTemplate restTemplate1 = new RestTemplate();
// HTTP 메세지 Header 생성하기
// exchange 메서드 활용
// 1. 헤더 구성
HttpHeaders headers = new HttpHeaders();
// 'Content-type': 'application/json; charset=UTF-8',
headers.add("Content-type", "application/json; charset=UTF-8");
// 2. 바디 구성
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("title", "안녕 반가워");
params.add("body", "후미진 어느 언던에서 도시락 먹기");
params.add("userId", "11");
// 3. 헤더와 바디 결합 --> HTTPEntity Object
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
// 4. RestTemplate 를 활용해서 HTTP 통신 요청
ResponseEntity<String> response = restTemplate1.exchange(uri, HttpMethod.POST, requestEntity, String.class);
System.out.println("response Header : " + response.getHeaders());
System.out.println("response Body : " + response.getBody());
return ResponseEntity.status(HttpStatus.CREATED).body(response.getBody());
}
} // end of HomeController()