- @ResponseBody ๋ฌธ์ ๋ฐํ
package hello.hello_spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.service.annotation.GetExchange;
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody // Http ํต์ ํ๋กํ ์ฝ์ body๋ถ์ ๋ด๊ฐ ์ง์ ๋ฃ์ด์ฃผ๊ฒ ๋ค๋ ๋ป
public String helloString(@RequestParam("name") String name) {
return "hello " + name; // viewํ์ด์ง๊ฐ ์๋ "hello string" ๋ฌธ์ ๊ทธ๋๋ก ๋์ด๊ฐ
}
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello; //JSON๋ฐฉ์์ผ๋ก ๊ฐ์ฒด ๋ฐํ
}
static class Hello {
private String name;
//getter, setter ๋จ์ถํค : alt + insertํค
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
@ResponseBody๋ฅผ ์ฌ์ฉํ๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ์ฌ์ฉํ์ง ์์
๋์ ์ HTTP BODY๋ถ์ ๋ฌธ์ ๋ด์ฉ์ ์ง์ ๋ฐํ
@ResponseBody๋ฅผ ์ฌ์ฉํ๊ณ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉด ๊ฐ์ฒด๊ฐ JSON์ผ๋ก ๋ณํ๋จ
@ResponseBody๋ฅผ ์ฌ์ฉ
- HTTP์ BODY์ ๋ฌธ์ ๋ด์ฉ์ ์ง์ ๋ฐํ
- 'ViewResolver' ๋์ ์ 'HTTPMessageConverter'๊ฐ ๋์
- ๊ธฐ๋ณธ ๋ฌธ์์ฒ๋ฆฌ : 'StringHttpMessageConverter'
- ๊ธฐ๋ณธ ๊ฐ์ฒด์ฒ๋ฆฌ : 'MappingJackson2HttpMessageConverter'
'๋ฐฑ์๋(Back-End) ๊ฐ๋ฐ > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SpringBoot] 3.2_ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ํ ์คํธ ์ผ์ด์ค ์์ฑ (0) | 2025.02.25 |
---|---|
[SpringBoot] 3.1_ํ์ ๋ฆฌํฌ์งํ ๋ฆฌ ๊ตฌํ (0) | 2025.02.25 |
[SpringBoot] 2.2_MVC์ ํ ํ๋ฆฟ ์์ง (0) | 2025.02.25 |
[SpringBoot] 2.1_์คํ๋ง ์น ๊ฐ๋ฐ ์ ์ ์ปจํ ์ธ (0) | 2025.02.24 |
[SpringBoot] 1.2_View ํ๊ฒฝ์ค์ ๋ฐ ๋น๋ํ๊ณ ์คํํ๊ธฐ (1) | 2025.02.24 |