JSCODE Logo
블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] 지역별 날씨 조회 외부 API Tool 구현

JSCODE 시니
JSCODE 시니
2026. 06. 13.
author
JSCODE 시니
category
Spring AI
createdAt
Jun 13, 2026 09:54 AM
isPublic
isPublic
series
실무에 바로 적용하는 Spring AI: Spring 서비스에 챗봇·RAG·MCP 도입하기
slug
practice-implementing-weather-api-as-spring-ai-tool
type
post
updatedAt
 

✅ 1. 지역별 날씨 조회 외부 API Tool 구현

Tools.java
package com.jscode.tool; @Service public class Tools { private final RestClient restClient; public Tools(RestClient.Builder restClientBuilder) { this.restClient = restClientBuilder.baseUrl("https://wttr.in").build(); } @Tool(description = "지역 이름을 받아 현재 날씨를 조회합니다.", returnDirect = true) public String getWeather(@ToolParam(description = "지역 이름") String location) { String customFormat = "현재 %l의 날씨는 %c 상태이며, 기온은 %t, 체감 기온은 %f, 풍속은 %w, 습도는 %h, 강수량은 %p입니다"; return restClient.get() .uri(uriBuilder -> uriBuilder .path("/{location}") .queryParam("format", customFormat) .queryParam("lang", "ko") .build(location)) .retrieve() .body(String.class); } }
 
 
 

✅ 2. 지역별 천문 정보 조회 외부 API Tool 구현

@Tool(description = "지역 이름을 받아 현재 3일간의 날씨와 천문 정보(달의 밝기, 달의 위상, 해/달의 뜨고 지는 시각) 를 조회합니다.") public WeatherResponse getWeatherDetails(@ToolParam(description = "지역 이름") String location) { return restClient.get() .uri(uriBuilder -> uriBuilder .path("/{location}") .queryParam("format", "j1") // json 출력으로 제공 .queryParam("lang", "ko") .build(location)) .retrieve() .body(WeatherResponse.class); // JSON 응답을 즉시 WeatherResponse Record 객체로 맵핑 } // Record(DTO) 클래스 public record WeatherResponse( @Schema(description = "일별(3일) 예보 정보 리스트") List<WeatherForecast> weather ) {} // 일별 예보 (hourly 제외) public record WeatherForecast( @Schema(description = "천문 정보(일출, 일몰 등)") List<Astronomy> astronomy, @Schema(description = "해당 날짜(yyyy-MM-dd)") String date, @Schema(description = "평균 기온(섭씨)") int avgtempC, @Schema(description = "평균 기온(화씨)") int avgtempF, @Schema(description = "최고 기온(섭씨)") int maxtempC, @Schema(description = "최고 기온(화씨)") int maxtempF, @Schema(description = "최저 기온(섭씨)") int mintempC, @Schema(description = "최저 기온(화씨)") int mintempF, @Schema(description = "일조 시간(시간 단위)") double sunHour, @Schema(description = "적설량(센티미터)") double totalSnow_cm, @Schema(description = "자외선 지수") int uvIndex ) {} // 천문 정보 public record Astronomy( @Schema(description = "달 밝기(%)") int moon_illumination, @Schema(description = "달의 위상(예: Full Moon 등)") String moon_phase, @Schema(description = "달 뜨는 시각") String moonrise, @Schema(description = "달 지는 시각") String moonset, @Schema(description = "해 뜨는 시각") String sunrise, @Schema(description = "해 지는 시각") String sunset ) {}