Notice
Recent Posts
Link
Recent Comments
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

지니의 개발로그

[Spring] @ResponseBody로 데이터 반환하기 본문

내일배움캠프

[Spring] @ResponseBody로 데이터 반환하기

지니♥︎ 2024. 5. 18. 21:14

🚩 20240517 TIL 내일배움캠프 #32일차

 

🔴 문제

(...생략)

@Controller
@RequestMapping("/response")

public class ResponseController {

@GetMapping("/json/class")
    public Star helloClassJson() {
        return new Star("jinny", 22);
    }
}

 

위 코드 실행 시

 

Error resolving template [response/json/class], template might not exist or might not be accessible by any of the configured Template Resolvers

 

🟠 분석

템플릿이 없다는 오류가 발생.

 

HTML반환하려는 것 아님,

그냥 데이터를 반환하려는 것이다라는 의미 전달 필요.

 

ResponseBody Annotation 추가하기

 

🟡 시도

@GetMapping("/json/class")
    @ResponseBody
    public Star helloClassJson() {
        return new Star("jinny", 22);
    }

 

🟢 배움

@Controller로 import한 경우

ResponseBody 애너테이션(Annotation) 누락 주의

 

cf.
@RestController를 import한 경우
@RestController = @Controller + @ResponseBody
@RestController를 사용하면 해당 클래스의 모든 메서드에 @ResponseBody 애너테이션이 추가되는 효과를 부여한다.