TIL - 2025-07-10
💡 새롭게 배운 것
- 프로젝트 하면서 생긴 이슈들과 검색 내용을 하나하나 적어보자!
- 그냥 넘어갔던 기초들부터 다시 공부해보자
❗ 오늘의 문제 상황 & 🚀 시도 및 해결 과정 & 배운점
- RestTemplate를 사용해 외부 서버로 파일 전송시
MultiValueMap 사용
- 파일 받기는 자동 파싱 되지만 보낼 경우 수동으로 multipart 형식을 만들어줘야댐
@Service
public class FileUploadService {
private final RestTemplate restTemplate;
public void uploadFile(MultipartFile file) {
// 1. 헤더 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// 2. MultiValueMap으로 수동 구성 (핵심!)
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("image", MultipartInputStreamFileResource.from(file));
// 3. 요청 전송
HttpEntity<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<>(body, headers);
restTemplate.postForEntity(url, requestEntity, ResponseDTO.class);
}
}
TIL - 2025-07-10
💡 새롭게 배운 것
❗ 오늘의 문제 상황 & 🚀 시도 및 해결 과정 & 배운점
MultiValueMap사용