250x250
Notice
Recent Posts
Recent Comments
«   2024/09   »
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
Tags more
Archives
Today
Total
관리 메뉴

개린이 개발노트

스프링 @RestController 본문

스프링Spring

스프링 @RestController

개린이9999 2022. 12. 10. 06:07
728x90

1. @RestController란?

- 스프링프레임워크 4.x 버전 이상부터 사용가능한 어노테이션으로 @Controller에 @ResponseBody가 결합된 어노테이션입니다. 컨트롤러 클래스에 @

RestController를 붙이면, 컨트롤러 클래스 하위 메서드에 @ResponseBody 어노테이션을 붙이지 않아도 문자열과 JSON 등을 전송할 수 있습니다.

 

2. @Controller와 @RestController 차이점

*@Restcontroller는 

Spring MVC Controller에 @ResponseBody가 추가된 것입니다. 따라서 @Controller와 달리 @RestController는 컨트롤러 클래스의 각 메서드마다 @ResponseBody를 추가할 필요가 없어졌습니다.

 

3. @RestController를 사용하여 문자열을 전송하는 방법

1
2
3
4
5
6
7
8
9
@RestController
@RequestMapping("/hello/*")
public class RestController {
    
    @RequestMapping("/test")
    public String test() {
        return "test";
    }
}
cs

 

4. @RestController를 사용하여 map을 전송하는 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RestController
@RequestMapping("/hello/*")
public class RestController {
    
    @RequestMapping("/test")
    public HashMap<StringObject> test() {
    
        HashMap<StringObject> hashMap = new HashMap<StringObject>();
    
        hashMap.put("name""john");
        hashMap.put("age""32");
        hashMap.put("gender""man");
        
        return hashMap;
    }
}
cs

참조

[Spring] 스프링 @RestController란? 그리고 사용방법 (tistory.com)

728x90