SPRING

[Spring Boot] @Controller / @GetMapping("/") / @ResponseBody

_-_-kk 2025. 3. 12. 21:20

서버기능을 만들고 싶으면 @Controller붙이기

controller는 보통 데이터나 html 보내는 역할임

 

@GetMapping("/") //url을 적으면 거기로 접속하면 데이터 보내줌

@ResponseBody를 쓰면 html같은게 아닌 return 옆에 있는걸 고대로 보내줌 

@ResponseBody를 안쓰면 경로를 보내줌

// 서버에서 데이터를 보내주고 싶으면 이 덩어리 가져다 쓰면 됨
@GetMapping("/") //url을 적으면 거기로 접속하면 데이터 보내줌
String hello(){
    return "index.html";
    //여러폴더중에 특정 파일을 꺼내고 싶을땐?  "폴더명/파일명"쓰면됨.
    //@ResponseBody == 문자 그대로 보내주세요

}

 

@GetMapping("/about")
@ResponseBody
String about(){
    return "갈치사이트";

}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<h4>메인페이지임</h4>
</body>
</html>

 

resources 폴더안의  static폴더에 html css 파일들을 보관함 

! 치고 tab키누르면 자동으로 html 양식 완성.