Spring

TIL Spring 1-2

๋ฆฐ์˜ˆ์กฐ 2023. 12. 5. 16:49

1. ์ •์ ํŽ˜์ด์ง€, ๋™์ ํŽ˜์ด์ง€

2. ๋ฐ์ดํ„ฐ๋ฅผ client ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

3. Path Variable & Request Param 

 

4. HTTP ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ


1. html ํŒŒ์ผ ๋ฐ˜ํ™˜

# ์ •์ ํŽ˜์ด์ง€

@Controller
public class HtmlController {

	//์ •์ ํŒŒ์ผ, static ํด๋”์— ์œ„์น˜!
    @GetMapping("/static-hello")
    public String hello(){

        return "hello.html";
    }

    @GetMapping("/html/redirect")
    public String htmlStatic(){
        return "redirect:/hello.html";
    } //thymeleaf ํ™˜๊ฒฝ์—์„œ controller ๋ฅผ ๊ฑฐ์น˜๋Š” ๋ฐฉ์‹

	//์ •์ ํŒŒ์ผ, templates ํด๋” ๋‚ด์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๊ธด ํ•จ.
    @GetMapping("/html/templates")
    public String htmlTemplates()
    {
        return "hello";  
    }
    
    //๊ฐ๊ฐ์˜ return ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ธฐ!

 

#๋™์ ํŽ˜์ด์ง€ ์ฒ˜๋ฆฌ 

@Controller
public class HtmlController {

	private static long vistCount=0;
    
    @GetMapping("html/dynamic")
    public String htmlDynamic(Model model){ //JAVA์—์„œ Model ๊ฐ์ฒด ์ œ๊ณต.
    	visitCount++;
        model.addAttribute("visit", visitCount); //๋ณ€๋™์‚ฌํ•ญ์ด ์ƒ๊ธด ๋ฐ์ดํ„ฐ๋ฅผ ์ ์šฉํ•˜๋Š” ๊ตฌ๊ฐ„
        return "html-visit";					//model.addAttribute() ์‚ฌ์šฉ!
    }

}

 

- ๋™์ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๋‘ ๊ฐ€์ง€ : model, view ์ด๋ฆ„์˜ ์ •๋ณด!!!


2. data client ๋ฐ˜ํ™˜ ๋ฐฉ๋ฒ•

- 1๋ฒˆ ๊ฐ™์€ ๊ฒฝ์šฐ html ๋“ฑ์˜ ํŒŒ์ผ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ. ์ด์ œ ์ ์  ํ”„๋ก ํŠธ์™€ ๊ฒฐํ•ฉ์ด ๋Š์Šจํ•œ ํ˜•ํƒœ๋กœ ๋ฐ”๋€Œ๋Š”๋ฐ

 ์ง€๊ธˆ ์‚ดํŽด๋ณผ ๊ฒฝ์šฐ๋„ ๊ทธ ์˜ˆ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

- html ํŒŒ์ผ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ x, JSON ํ˜•ํƒœ๋กœ ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•ํƒœ!

 

@Controller
@RequestMapping("/response")
public class ResponseController {

	#1
    @GetMapping("/json/string")
    @ResponseBody
    public String helloStringJson() {
        return "{\"name\":\"Robbie\",\"age\":95}";
    }
    
	#2
    @GetMapping("/json/class")
    @ResponseBody
    public Star helloClassJson() { //Star ํด๋ž˜์Šค ํ•„์š”
        return new Star("Robbie", 95);
    }
}

 

#JSON ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜ ๋ฐฉ๋ฒ•

 

#1. ๋ฐ˜ํ™˜ ๊ฐ’: String -> JSON ํƒ€์ž… ์„œ๋ฒ„๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์—†์Œ -> JSON ํ˜•ํƒœ์˜ String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉ 

 

#2 java ๊ฐ์ฒด 

-> ํด๋ผ์ด์–ธํŠธ๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ 1๋ฒˆ์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ json ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ 

 Spring์—์„œ ํ•ด๋‹น ์ž‘์—…์„ ์•Œ์•„์„œ ํ•ด์ค€๋‹ค! Star ํด๋ž˜์Šค์— ์ •์˜๋œ ๊ฐ’์„ key, retrun ์— ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ value ๊ฐ’์œผ๋กœ  !

(Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)

 

-> ResponseBody ๋˜ํ•œ ๋ฐ˜๋ณต๋œ๋‹ค ! ์ค„์—ฌ๋ณด์ž!

 @RestController ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ด๋Š” ResponseBody + Controller ๋‘๊ฐ€์ง€๊ฐ€ ํ•ฉ์ณ์ง„ ๊ฒƒ!

 ๋ฉ”์„œ๋“œ์— ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  @Controller ๋Œ€์‹  ํ•ด๋‹น ์œ„์น˜์— @RestController ์ ์–ด์ฃผ๋ฉด ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๊ฒŒ ๋œ๋‹ค!


 

3. Path Variable(์ƒ๋žต x) & Request Param(์ƒ๋žตO)

- ํด๋ผ์ด์–ธํŠธ ์ธก ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„๋กœ HTTP์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 

 

#1. Path Variable

- ์„œ๋ฒ„์— ๋ณด๋‚ด๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ URL์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

- @PathVariable

 

#2. Request Param (์ฟผ๋ฆฌ)

- key, valuie ํ˜•์‹ 

- ์„œ๋ฒ„์— ๋ณด๋‚ด๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ URL ๊ฒฝ๋กœ ๋งˆ์ง€๋ง‰์— ?์™€ &๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

- @RequestParam 

ex) ?name=Robbie&age=95 

 

// GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95

@GetMapping("/form/param")
@ResponseBody
public String helloGetRequestParam(@RequestParam String name, @RequestParam int age) {
    return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);
}

-> ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ฐฉ๋ฒ•!

 ํ•ด๋‹น ์š”์ฒญ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์— @RequestParam ์• ๋„ˆํ…Œ์ด์…˜ ! ๊ทธ๋ฆฌ๊ณ  key ๋ถ€๋ถ„์— ์„ ์–ธํ•œ ๋ณ€์ˆ˜ ํƒ€์ž…๊ณผ ๋ณ€์ˆ˜๋ช…์„ ์„ ์–ธํ•ด์ฃผ๋ฉด ๋œ๋‹ค~!

 

์ด๋•Œ, ํ•ด๋‹น ์• ๋„ˆํ…Œ์ด์…˜์€ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•œ๋ฐ ์š”์ฒญ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์—†์„ ๋• ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

๊ทธ๋Ÿฌ๋‚˜ ๋‹น์—ฐํžˆ ๋ฐ์ดํ„ฐ ์—†์ด ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค! 

๊ทธ๋Ÿด ๋• ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ถ€๋ถ„ @RequestParam(required=false) ๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์—†์–ด๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค ~~

 


4. HTTP ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ

- ModelAttribute (์ƒ๋žตo), RequestBody (์ƒ๋žต x)

-> ์ด์ „์—๋Š” POST ๋ฐฉ์‹์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์—์„œ Body์— ์ฟผ๋ฆฌ ํ˜•์‹์œผ๋กœ ๋‹ด๊ธด ๋ฐ์ดํ„ฐ๋ฅผ RequestParam์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•จ. 

 

1. @ModelAttribute

Body ๋ถ€๋ถ„์— ๋“ค์–ด์˜จ ์ฟผ๋ฆฌ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  Spring ๋‚ด๋ถ€์—์„œ ์ž๋™์œผ๋กœ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

@ModelAttribute ์• ๋„ˆํ…Œ์ด์…˜์ด ํ•„์š”ํ•จ.

 

1) html form ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(POST) ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Œ(์–ด์ฐจํ”ผ ์ฟผ๋ฆฌ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋จ.)

 

2) QueryString(GET)

- ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์€ ๊ฒฝ์šฐ์—๋Š” ๊ดœ์ฐฎ์œผ๋‚˜ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ @RequestParam์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฐ›๊ธฐ์— ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ด๊ฒƒ๋„ ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ~ 

// [Request sample]
// GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95
@GetMapping("/form/param/model")
@ResponseBody
public String helloRequestParam(@ModelAttribute Star star) {
    return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}

 

์ฃผ์˜! ํ•ด๋‹น ์• ๋„ˆํ…Œ์ด์…˜๋„ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค..!

 

๊ทธ๋ ‡๋‹ค๋ฉด @RequestParam & @ModelAttribute ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•œ ์ด ๋‘๊ฐ€์ง€๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ• ๊นŒ?

- ํŒŒ๋ผ๋ฏธํ„ฐ ๋ถ€๋ถ„์ด ํด๋ž˜์Šค๋‚˜ Object ์ผ ๊ฒฝ์šฐ์—๋Š” @ModelAttribute ๋กœ ์ธ์‹

- SimpleValueType(์›์‹œํ˜•, wrapper, Date)์ผ ๊ฒฝ์šฐ์—๋Š” RequestParam์œผ๋กœ ์ธ์‹ํ•œ๋‹ค. 

 

2. @RequestBody

HTTP body ๋ถ€๋ถ„์— JSON ํ˜•ํƒœ๋กœ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๋Š” @RequestBody ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค! (์ƒ๋žต ๋ถˆ๊ฐ€!)

// [Request sample]
// POST http://localhost:8080/hello/request/form/json
// Header
//  Content type: application/json
// Body
//  {"name":"Robbie","age":"95"}
@PostMapping("/form/json")
@ResponseBody
public String helloPostRequestJson(@RequestBody Star star) {
    return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
}

 

 

 

 

'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL Spring #2-3  (0) 2023.12.12
TIL Spring 2-2  (0) 2023.12.08
TIL Spring 2-1  (1) 2023.12.07
TIL Spring 1-3  (0) 2023.12.06
TIL Spring #1-1  (0) 2023.12.04