Develop

36일차_공공데이터 (Open API) + Spring Cloud (OpenFeign) 연동 실습 본문

백엔드/KDT_Programmers

36일차_공공데이터 (Open API) + Spring Cloud (OpenFeign) 연동 실습

230801 2025. 4. 29. 09:39

안녕하세요 

오늘은 데브코스 36일차(8주 2일차) 입니다. 

 

서울시 공공데이터를 API 연동해서 Spring Cloud 실습을 진행했습니다. 

 

1. 데이터 찾기

: 서울시 API 인증키 발급

 

2. Spring Cloud 시작

Spring Cloud 란 ?
마이크로서비스(MSA) 아키텍처를 쉽게 만들 수 있도록 도와주는 Spring 프로젝트 모음집.
(ex: 서비스 등록, API 호출, Config 서버 구축 등)

 

 

Spring Cloud Train Reference Documentation :: Spring Cloud Release

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus). Coordination of distributed

docs.spring.io

 

Spring Cloud를 쓰는 이유

  • RestTemplate 보다 코드가 간결함
    • RestTemplate : 스프링에서 외부 HTTP 요청을 보내기 위해 사용하는 오래된 방식의 클라이언트(자바판 Postman 역할)
  • 요청/응답 DTO 만 만들면 API 호출 가능
  • 인터페이스 기반이라 테스트도 편함

 

OpenFeign이란?

HTTP 통신을 인터페이스만 만들면 끝나게 해주는 라이브러리
(복잡한 RestTemplate 코드 없이, 인터페이스 메서드만 짜면 API 호출 가능)

OpenFeign 을 이용해 외부 API 호출을 인터페이스 기반으로 구현

 

 

1. pom.xml dependencyManagement 추가

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>2024.0.1</version> <!-- Spring Boot 버전에 맞게 조정 -->
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

 

 

 

2. OpenFeign 의존성 추가

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 

 

 

3. XML 파싱용 라이브러리 추가 (에러 대응용)

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.18.3</version>
</dependency>
  • API 응답이 XML 형식이면, JSON처럼 쉽게 파싱하려고 추가

 

 

 

3. API 연동 준비

  • Postman으로 api 연동 사전 테스트
  • @EnableFeignClients → 메인 클래스에 추가 (Feign 인터페이스 스캔용)
  • API 응답 데이터(JSON/XML)을 받아올 수 있도록 DTO 클래스 생성
    • @JsonProperty("NODE_ID") 같은 어노테이션으로 JSON key 매핑
    • XML 데이터도 처리할 수 있도록 Jackson XML Parser 추가
  • 응답 받을 필드도 모두 세팅

 

4. Feign 메서드 작성

 

5. MyBatis 작성시 주의사항

<if test="search == 'author'">
  or author like concat('%', #{keyword}, '%')
</if>
  • #{} 사용해야 SQL Injection 공격을 방어할 수 있음

 

 

6. 1차 프로젝트 시작

  • 기존 팀과 어느정도 기획된 프로젝트를 일주일간 진행하게 됨 .ᐟ
    • 구조 잡기 (패키지 설계, 기본 뼈대 세팅)
    • 설계하기 (기능별 책임 나누기, API 흐름 잡기)

 

 

감사합니다.