티스토리 뷰
오픈API - 한국천문연구원_특일 정보
오픈API를 사용해보자 해서 시작하게되었다.현재 진행한 블로그에서메인페이지에 공휴일 체크 INPUT BOX + BUTTON 만 추가해서ALERT로만 간단하게 나타내도록 하려고한다. 1. UTIL.KT 2. MAIN.JS처리하는
ekeprl.tistory.com
이 포스터에서는 특일정보API를 프론트에서 받아서 처리하는 방식으로 단순하게 구현했다.
JS에서 처리하는게 전부가아닌, 백엔드를 거쳐 처리하도록 만드는 와중에 오류가 발생했다.
ERROR io.undertow.request - UT005023: Exception handling request to /api/holiday
가장 먼저 보이는 이 오류와함께
Request processing failed: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/api/holiday.html]")
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1022)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)
부연설명까지.
해당 오류를 확인해보면
Spring MVC가 Thymeleaf 템플릿을 찾으려다 예외 오류가 발생했다는 뜻이다.
이 오류를 보고 Controller, Service를 확인했는데
1. Controller
@RequestMapping(value = [("/api/holiday")])
@Throws(Exception::class)
fun getHolidays(@RequestBody request: HolidayModel): JSONObject {
return service.getHolidays(request)
}
2. Service
@Throws(Exception::class)
fun getHolidays(request: HolidayModel): JSONObject {
val jsono = JSONObject()
try {
val year = request.Year
val month = request.Month
val apiKey = env.getProperty("apis.data.holiday.servicekey")
val apiUrl = "https://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo" +
"?solYear=$year&solMonth=$month&ServiceKey=$apiKey&_type=json"
val url = java.net.URL(apiUrl)
val connection = (url.openConnection() as HttpsURLConnection).apply {
requestMethod = "POST"
setRequestProperty("Content-Type", "application/json")
setRequestProperty("Accept", "application/json")
doOutput = true
}
val responseCode = connection.responseCode
if (responseCode == 200) {
val response = connection.inputStream.bufferedReader().readText()
jsono.put("RESULT", "OK")
jsono.put("MESSAGE", "휴일 정보를 정상적으로 가져왔습니다.")
jsono.put("DATA", response) // JSON 객체로 담기
} else {
jsono.put("RESULT", "ERR")
jsono.put("MESSAGE", "API 요청 실패: HTTP $responseCode")
}
} catch (e: Exception) {
logger.error("ERROR", e)
jsono.put("RESULT", "ERR")
jsono.put("MESSAGE", "오류 발생: ${e.message}")
}
return jsono
}
분명히 서비스단에서 JSONobject로 반환을 시켰는데
뜬금없이 Thymeleaf 템플릿을 찾으려다 예외 오류가 발생했다는게 이상했다.
이런 컨트롤러에서
@ResponseBody 어노테이션을 빼먹었다.
어노테이션 하나를 빼먹었는데 그로인해서
- Controller 메서드에 @RequestMapping만 붙이고, @ResponseBody 또는 @RestController를 사용하지 않음.
- POST 요청에 대해 Spring이 기본적으로 View를 찾으려고 시도 → templates/api/holiday.html을 찾으려다 실패.
- @RequestBody로 JSON을 받았지만, 반환 타입이 단순 JSONObject라서 Spring이 이를 View 이름으로 해석하려고 시도함.
이런 이유로 json으로 받지못하고 오류가 발생했던것이다.
3. 수정한 Controller
@RequestMapping(value = [("/api/holiday")])
@ResponseBody ==> 추가한 코드
@Throws(Exception::class)
fun getHolidays(@RequestBody request: HolidayModel): JSONObject {
return service.getHolidays(request)
}
컨트롤러를 수정하니 오류가 해결되었다.
이상 UT005023: Exception handling request to /api/* (Thymeleaf템플릿 예외오류)에 대해 알아보았다.
다음은 해당 기능을 완성하여 백엔드를 통한 특일정보API 포스팅을 진행하려한다.
'Project > Error' 카테고리의 다른 글
| [IntelliJ] 명령줄이 너무 깁니다 / Command line is too long 해결 (0) | 2026.05.12 |
|---|---|
| Column count doesn't match value count at row 1 (0) | 2025.01.14 |
| Bootstrap + Js 오류(bootstrap is not defined) (0) | 2024.12.24 |
| Exception in thread "main" java.lang.AbstractMethodError: (0) | 2024.09.02 |
| Error페이지 설정 (0) | 2024.08.22 |
- Total
- Today
- Yesterday
- dbeaver
- gradle
- JAR매니패스트
- session저장
- 게시판
- MariaDB
- Powershell
- Git
- 백엔드
- InetAddress
- springsecurity
- AI
- jwt
- 명령줄이 너무 깁니다
- Postman
- Insert오류
- CRUD
- 의존성
- kotlin
- ubuntu
- Spring Security
- securityconfig
- GitHub
- 서버호스트
- 특일정보
- insert
- Column count doesn't match value count at row 1
- null
- dependencies
- spring ai
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
