Tiny Bunny
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/SpringBoot

[Spring Boot] ์ปจํŠธ๋กค๋Ÿฌ / @annotation ์ •๋ฆฌ +๊ฐ„๋‹จํ•œ viewํŒŒ์ผ ๋„˜๊ธฐ๊ธฐ

by soonybutter 2024. 12. 14.
728x90

 

 

 

 

 

 

 

์ด๋ฒˆ๊ธ€์€ Controller ์— ๋Œ€ํ•ด ์ข€๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ณ  Index ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.

Controller๋ž€ MVC์—์„œ C์— ํ•ด๋‹นํ•˜๋ฉฐ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ํ›„ V์— ํ•ด๋‹น๋˜๋Š” ๋ทฐ์— M์— ํ•ด๋‹น๋˜๋Š” ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•œ๋‹ค. Controller ์˜ ๋Œ€ํ‘œ์ ์ธ ์ด๋…ธํ…Œ์ด์…˜์€ ์–ด๋–ค๊ฒŒ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

@Controller

Controller ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๋ช…์‹œํ•œ๋‹ค.
๋ช…์‹œ๋œ ํด๋ž˜์Šค๋ฅผ Spring MVC ์ปจํŠธ๋กค๋Ÿฌ๋กœ ํ‘œ์‹œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

์ฃผ๋กœ View์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
@ResponseBody ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ํด๋ž˜์Šค ์„ ์–ธ๋ถ€์— ์ž…๋ ฅํ•œ๋‹ค.

@Controller public class TestRestController { }

โ€‹

 

@RequestMapping

ํŠน์ • URI ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด Controller ์—์„œ ์–ด๋–ค ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ง€ ๋งตํ•‘ํ•˜๊ธฐ ์œ„ํ•œ ์ด๋…ธํ…Œ์ด์…˜์ด๋‹ค.
์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ์ด๋…ธํ…Œ์ด์…˜์„ ๊ธฐ์ž…ํ•œ๋‹ค.

 

RequestMapping(value="/users", method=@RequestMethod.GET) -> @GetMapping("/users") // ๋™์ผ 
@RequestMapping(value="/users", method=@RequestMethod.POST) -> @PostMapping("/users") // ๋™์ผ 
@RequestMapping(value="/users", method=@RequestMethod.PUT) -> @PutMapping("/users") // ๋™์ผ 
@RequestMapping(value="/users", method=@RequestMethod.DELETE) -> @DeleteMapping("/users") // ๋™์ผ

@RequestBody

HTTP ์š”์ฒญ์˜ Body ๋‚ด์šฉ์„ ํ†ต์งธ๋กœ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋งคํ•‘๋œ ๋ฉ”์†Œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ด์ค€๋‹ค.

@Controller @RequestBody public class TestRestController { }

@ResponseBody

์ž๋ฐ” ๊ฐ์ฒด๋ฅผ HTTP์š”์ฒญ์˜ Body ๋‚ด์šฉ์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•œ๋‹ค.

@Controller 
public class TestRestController { 
    @ResponseBody 
    @RequestMapping("/hello") 
        public String hello(){
            return "Hello"; 
} }

 

@RestController

RESTful API ์„œ๋น„์Šค์— ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ์ปจํŠธ๋กค๋Ÿฌ์ด๋ฉฐ @Controller + @Response ํ•ฉ์ณ ๋†“์€๊ฑฐ๋ž‘ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค.

Spring MVC Controller์— @ResponseBody๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์œผ๋ฉฐ JSON ํ˜•ํƒœ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

@RestController public class TestRestController { }
 

 

 

 

 

์—ฌ๊ธฐ๊นŒ์ง€ Controller ์ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์ด์ œ Index ํŽ˜์ด์ง€๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ๋ณผ ์˜ˆ์ •์ด๋‹ค.

โ€‹

1. Index view ์ƒ์„ฑํ•˜๊ธฐ

์†Œ์Šค ๊ฒฝ๋กœ src/main/resoreces/static ์— index.html ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. static ํด๋”๋Š” html ๋ฌธ์„œ, ์ด๋ฏธ์ง€, ์˜์ƒ ๋“ฑ ์ •์  ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•˜๋Š” ํด๋”์ด๋‹ค.

์ƒ์„ฑํ•œ index.html ๋ฌธ์„œ๋ฅผ ์—ด์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•ด ๋ณด์ž.

Controller ์„ ์ž‘์„ฑํ•œ๋‹ค.

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
@Controller 
public class TestController { 
    @RequestMapping(value = "/index") 
        public String index(){ 
            return "index.html"; 
} }

index.html ์™€ TestController ํด๋ž˜์Šค ์ž‘์„ฑ ํ›„ ์‹คํ–‰ํ•ด์„œ url๋กœ ์ ‘์†ํ•ด ๋ณด๋ฉด Hellow World! ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

Ajax ํ†ต์‹ ์„ ์ด์šฉํ•˜์—ฌ Html ๋ฌธ์„œ์—์„œ Object ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

index.html์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

<!DOCTYPE html> 
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script> 
    $.ajax({ 
    type: "GET", 
    url: "/getValue", 
    success: (data) => { console.log(data); $('#contents').html(data); 
    } }); 
</script> 
</head> 
<body> 
    <h1>Hello World!</h1> 
    <div id="contents"> </div> 
</body> 
</html>

getValue ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ TestController ํด๋ž˜์Šค๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
@Controller 
public class TestController { 
    @RequestMapping(value = "/index") 
    public String index(){ 
        return "index.html"; 
    } 
    
    @ResponseBody 
    @RequestMapping("/getValue") 
    public String getValue() { 
        return "Hello java spring boot!"; 
} }

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ์‹คํ–‰ํ•ด์„œ ๋‹ค์‹œ ํ™•์ธํ•ด ๋ณด๋ฉด "Hello java spring boot! ๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜จ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ€‹

View ํ˜•์‹ ๋ง๊ณ ๋„ @RestController ์„ ์ด์šฉํ•˜์—ฌ JSON ํ˜•ํƒœ๋กœ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

728x90

TOP

Designed by ํ‹ฐ์Šคํ† ๋ฆฌ