
ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์๊ฒ ์์ฒญ์ ํ๊ณ ์๋ฒ์์ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ตํ ๋ ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํํ์ฌ JSON์ผ๋ก ๋ณด๋ด๋ฉด axios๋ ํ์ฑํด์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ง๋ค์ด ํ๋ฉด์ ํ์ํฉ๋๋ค.
React(App.jsx)
import React, {useState} from "react";
import axios from "axios";
function App(props) {
const [result, setResult] = useState("");
function handleClick2() {
axios.get("/api/main42/sub2").then((param) => setResult(param.data));
}
return (
<div>
<button onClick={handleClick2}>์๋ต ๋ฐ๊ธฐ</button>
</div>
);
}
export default App;
handleClick2 ํจ์๊ฐ ํธ์ถํ๊ณ axios๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ๋ก GET ์์ฒญ์ ๋ณด๋ด๊ณ , ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ฝ์์ ์ถ๋ ฅํฉ๋๋ค..then() ๋ฉ์๋๋ ์์ฒญ์ด ์๋ฃ๋๊ณ ์๋ต์ด ๋์ฐฉํ์ ๋ ์คํ๋ ์ฝ๋ฐฑ ํจ์์
๋๋ค.then() ๋ฉ์๋์ ๋งค๊ฐ๋ณ์ param์ ์๋ต ๊ฐ์ฒด์ด๋ฉฐ param.data๋ ์๋ฒ์์ ๋ฐํํ ์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ํ๋
๋๋ค. Spring Boot(Controller42.java)
@Controller
@RequestMapping("/api/main42")
public class Controller42 {
@GetMapping("sub2")
@ResponseBody
public String method2() {
double random = Math.random();
if (random < 1.0 / 3) {
return "๊ฐ์";
} else if (random < 2.0 / 3) {
return "๋ฐ์";
}
return "๋ณด";
}
}
@ResponseBody ์ด๋
ธํ
์ด์
์ด ์๊ณ return์ ํ๋ค๋ฉด ๋ฐ๋ก view๋ก ๋ณด๋ด๊ธฐ ๋๋ฌธ์ ์๋ต ๋ณธ๋ฌธ์ผ๋ก ๋ณด๋ด๊ธฐ ์ํด @ResponseBody` ์ด๋
ธํ
์ด์
์ด ์์ด์ผ ํฉ๋๋ค./api/main42/sub2 ๊ฒฝ๋ก๋ก GET ์์ฒญ์ ๋ณด๋ด๋ฉด, ์ด ๋ฉ์๋๊ฐ ๊ฐ์, ๋ฐ์, ๋ณด ๋ฌธ์์ด์ ๋๋คํ๊ฒ ์๋ตํฉ๋๋ค. ์ด ์๋ต์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ค์ ์ ๋ฌ๋์ด ํ๋ฉด์ ํ์ํฉ๋๋ค.์คํ๋ง์์ map ๊ฐ์ฒด๋ก ์๋ตํ ๊ฒ์ JSON์ผ๋ก ๋ฐ๊พธ๊ณ JSON์ ๊ฐ์ฒด๋ก ๋ฐ๊ปด์ ํ๋ฉด์ ํ์๋ฉ๋๋ค.
React(App.jsx)
import React, {useState} from "react";
import axios from "axios";
function App(props) {
function handleClick4() {
axios.get("/api/main42/sub4").then((param)=> console.log(param.data));
}
return (
<div>
<button onClick={handleClick4}>JSON ์๋ต ๋ฐ๊ธฐ</button>
</div>
);
}
export default App;
handleClick2() : ์๋ฒ๋ก GET ์์ฒญ์ ๋ณด๋ด๊ณ , ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต ๋ฐ์ดํฐ(param.data)๋ฅผ JSON ํ์์ ๋ง๊ฒ ์ฝ์์ ์ถ๋ ฅํฉ๋๋ค.{
"name": "ํ๊ธธ๋",
"age": 22,
"married": false,
"info": {
"team": "ํ ํธ๋",
"location": "๋ฐ๋"
},
"item": ["์ปดํจํฐ", "์ค๋งํธํฐ"]
}
Spring Boot(Controller42.java)
@Controller
@RequestMapping("/api/main42")
public class Controller42 {
@GetMapping("sub3")
@ResponseBody
public Map<String, Object> method3(){
return Map.of("name", "ํ๊ธธ๋", "age", 22,
"married", false,
"info", Map.of("team", "ํ ํธ๋", "location", "๋ฐ๋"),
"item", List.of("์ปดํจํฐ", "์ค๋งํธํฐ"));
}
}
/api/main42/sub3 GET ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , Map ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ JSON ํ์์ ์๋ต์ ๋ณด๋
๋๋ค. ์ด ์๋ต์ "name", "age", "married", "info", "item" ๋ฑ์ ํค์ ๊ทธ์ ํด๋นํ๋ ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.React(App.jsx)
import React, {useState} from "react";
import axios from "axios";
function App(props) {
function handleClick5() {
axios.get("/api/main42/sub5").then((param)=> console.log(param.data));
}
return (
<div>
<button onClick={handleClick5}>JSON ์๋ต ๋ฐ๊ธฐ</button>
</div>
);
}
export default App;
handleClick5() : ์๋ฒ๋ก GET ์์ฒญ์ ๋ณด๋ด๊ณ , ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต ๋ฐ์ดํฐ(param.data)๋ฅผ JSON ํ์์ ๋ง๊ฒ ์ฝ์์ ์ถ๋ ฅํฉ๋๋ค.{
"name": "๊ฐ์ธ",
"height": 177.7,
"info": {
"name": "ํฅ๋ฏผ",
"age": 55,
"desc": "ํฅ๋ฏผ๋์ 55์ด์ ๋๋ค."
},
"foods": [
"๊ฐ์",
"๊ณ ๊ตฌ๋ง"
],
"married": false
}
์๋ฐ๋น(MyBean411.java)
@Data
public class MyBean411 {
private String name;
private Integer age;
public String getDesc(){
return name + "๋์ " + age + "์ด์
๋๋ค.";
}
}
get ๋ฉ์๋๊ฐ ์์ผ๋ฉด ํด๋น ๋ฉ์๋์ ์ํด ๊ฒฐ์ ๋ ํ๋กํผํฐ์ ๊ฐ์ด JSON์ผ๋ก ์๋ต๋ ์ ์์ต๋๋ค.Spring Boot(Controller42.java)
@Controller
@RequestMapping("/api/main42")
public class Controller42 {
@GetMapping("sub5")
@ResponseBody
public MyBean411 method5(){
MyBean411 obj = new MyBean411();
obj.setName("ํ๊ธธ๋");
obj.setAge(22);
return obj; // JSON์ผ๋ก ์ง๋ ฌํ
}
}
/api/main42/sub3 GET ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , Map ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ JSON ํ์์ ์๋ต์ ๋ณด๋
๋๋ค. ์ด ์๋ต์ "name", "age", "married", "info", "item" ๋ฑ์ ํค์ ๊ทธ์ ํด๋นํ๋ ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.