[ ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ(2) ]

 

Spring - [intelliJ,Thymeleaf] ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋ค ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ(2)

[ ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ ] Spring - [intelliJ,Thymeleaf] ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋ค ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ[ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด Hello,World! ์ถœ๋ ฅ ] Spring - [intelliJ] ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด Hello,World! ์ถœ๋ ฅ[ Hello,World

post-this.tistory.com

 

 


 

 

@PostMapping์„ ์ด์šฉํ•˜์—ฌ
๋กœ๋˜ ๋ฒˆํ˜ธ ๊ณ„์† ์ถœ๋ ฅํ•˜๊ธฐ.



1. ๋™์ž‘ํ™”๋ฉด๊ณผ ์ „์ฒด์ฝ”๋“œ

 

 

๋™์ž‘ํ™”๋ฉด

 

 

 

 

 

์ „์ฒด์ฝ”๋“œ

 

GitHub - hyeong-ing/LuckyLotto: ๋กœ๋˜ ๋ฒˆํ˜ธ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์›น์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค.

๋กœ๋˜ ๋ฒˆํ˜ธ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์›น์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. Contribute to hyeong-ing/LuckyLotto development by creating an account on GitHub.

github.com

 

 

 

package com.LottoWeb.LuckyLotto.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.*;

@Controller
public class RandomController {

    java.util.Random rd = new java.util.Random();

    @GetMapping("/")
    public String firstPage() {
        return "button";
    }

    

    @GetMapping("/Random1")
    public String random1(Model model) {

        Set<Integer> lottoSet = new TreeSet<>();

        while(true) {
            int ranNum = rd.nextInt(45)+1;
            lottoSet.add(ranNum);

            if(lottoSet.size() == 6) {
                break;
            }
        }

        List<Integer> lottoList = new ArrayList<>(lottoSet);

        model.addAttribute("Number1",lottoList.get(0));
        model.addAttribute("Number2",lottoList.get(1));
        model.addAttribute("Number3",lottoList.get(2));
        model.addAttribute("Number4",lottoList.get(3));
        model.addAttribute("Number5",lottoList.get(4));
        model.addAttribute("Number6",lottoList.get(5));


        return "button1";
    }

    @PostMapping("/Random1")
    public String updateRandom(Model model) {

        Set<Integer> lottoSet = new TreeSet<>();

        while(true) {
            int ranNum = rd.nextInt(45)+1;
            lottoSet.add(ranNum);

            if(lottoSet.size() == 6) {
                break;
            }
        }

        List<Integer> lottoList = new ArrayList<>(lottoSet);

        model.addAttribute("Number1",lottoList.get(0));
        model.addAttribute("Number2",lottoList.get(1));
        model.addAttribute("Number3",lottoList.get(2));
        model.addAttribute("Number4",lottoList.get(3));
        model.addAttribute("Number5",lottoList.get(4));
        model.addAttribute("Number6",lottoList.get(5));


        return "button1";
    }

}

 

 

 

 


 

 

 

 

2. ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์ง€ ์ƒ๊ฐํ•˜๊ธฐ ๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป

 

์ „์— ํ–ˆ๋˜ ์ฝ”๋“œ๋Š” @GetMapping๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋˜๋ฒˆํ˜ธ๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ @PostMapping์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๋ฐ”๊ฟ€ ๊ฒƒ์ด๋‹ค.

 

 

[ ๋ณ€๊ฒฝ์‚ฌํ•ญ ]

 

โ–ก @GetMapping์œผ๋กœ ์ฒ˜์Œ ๊ฐ’์„ ์ถœ๋ ฅ์‹œํ‚จ๋‹ค.

โ–ก @PostMapping์„ ์ด์šฉํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

 

 

 

 


 

 

 

 

3. @GetMapping์œผ๋กœ ์ฒซ ์ถœ๋ ฅํ•˜๊ธฐ.

 

โ–ก ์ฒ˜์Œ ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ์ƒˆ๋กœ์šด html์„ ๋ณด๋‚ด๋Š” GET์š”์ฒญ ์ฒ˜๋ฆฌ.

@GetMapping("/Random1")
public String random1(Model model) {

    Set<Integer> lottoSet = new TreeSet<>();

    while(true) {
        int ranNum = rd.nextInt(45)+1;
        lottoSet.add(ranNum);

        if(lottoSet.size() == 6) {
            break;
        }
    }

    List<Integer> lottoList = new ArrayList<>(lottoSet);

    model.addAttribute("Number1",lottoList.get(0));
    model.addAttribute("Number2",lottoList.get(1));
    model.addAttribute("Number3",lottoList.get(2));
    model.addAttribute("Number4",lottoList.get(3));
    model.addAttribute("Number5",lottoList.get(4));
    model.addAttribute("Number6",lottoList.get(5));


    return "button1";
}

 

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—๋Š” ์›ํ˜• ๋ชจ์–‘์ด ์—†๋Š” button.html ํ™”๋ฉด๊ณผ ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค ๋žœ๋ค ์ˆซ์ž๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” button1.html ํ™”๋ฉด์ด ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ํ™”๋ฉด์„ ๋“ค์–ด๊ฐ€๋ ค๋ฉด GET ์š”์ฒญ์œผ๋กœ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ฌด์กฐ๊ฑด ์ฒซ ํ™”๋ฉด์„ ๋ฐ›๋Š” ๊ฒƒ์œผ๋กœ @GetMapping์ด ์žˆ์–ด์•ผํ•˜๋Š” ์ด์œ ๋‹ค. 

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ์ƒˆ๋กœ์šด ํ™”๋ฉด์„ ์š”์ฒญํ•˜๋Š” ๋™์‹œ์—, ๋žœ๋ค ๋ฒˆํ˜ธ๋ฅผ ๋ฐ›์•„ ๊ทธ๊ฒƒ์„ ์›ํ˜• ์œ„์— ์ถœ๋ ฅํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

 

 


 

 

 

 

 

4. @PostMapping์œผ๋กœ ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ.

   @PostMapping("/Random1")
    public String updateRandom(Model model) {

        Set<Integer> lottoSet = new TreeSet<>();

        while(true) {
            int ranNum = rd.nextInt(45)+1;
            lottoSet.add(ranNum);

            if(lottoSet.size() == 6) {
                break;
            }
        }

        List<Integer> lottoList = new ArrayList<>(lottoSet);

        model.addAttribute("Number1",lottoList.get(0));
        model.addAttribute("Number2",lottoList.get(1));
        model.addAttribute("Number3",lottoList.get(2));
        model.addAttribute("Number4",lottoList.get(3));
        model.addAttribute("Number5",lottoList.get(4));
        model.addAttribute("Number6",lottoList.get(5));


        return "button1";
    }

 

๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค ์š”์ฒญ์ด ๋‹ค์‹œ ๊ฐ€๊ฒŒ๋˜๋ฉด @PostMapping์ด ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•œ๋‹ค. ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋Š” ๋˜‘๊ฐ™๋‹ค. @PostMapping์€ ์ƒˆ Form ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ฑฐ๋‚˜, ํŠน์ • ๋ฆฌ์†Œ์Šค ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. Rest API์—์„œ ๊ฐ’์€ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํ•˜๋‚˜๋Š” @PostMapping, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” @PutMapping์ด ๋œ๋‹ค.

 

๋‘˜์˜ ์ฐจ์ด๋Š” @PostMapping์€ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ ์—…๋ฐ์ดํŠธ ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. @PutMapping์€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ํ˜„์žฌ ์ฝ”๋“œ๋Š” ์‰ฌ์šด ์ฝ”๋“œ๋ผ์„œ, ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. @GetMapping์€ GET๋ฐฉ์‹์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์ฒ˜๋ฆฌ๋งŒ ํ•œ๋‹ค. ์„œ๋ฒ„์˜ ์ƒํƒœ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ๊ฐ’์„ ๊ณ„์†์ ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ–ˆ์„ ๋•Œ, ํ™”๋ฉด์„ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค์–ด @GetMapping์ด ๊ณ„์† ๋‹ค๋ฅธ ํ™”๋ฉด์œผ๋กœ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

 


 

 

๐Ÿชฝ

"ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

 

 

 

[ ๋ธ”๋กœ๊ทธ์— ์“ด ๋‚ด์šฉ๋“ค์˜ ์ถœ์ฒ˜๋Š” ์•„๋ž˜์— ์ ์–ด๋‘˜ํ…Œ๋‹ˆ

๋“ค์–ด๊ฐ€์„œ ์ฝ์–ด๋ณด์‹œ๋ฉด ๋”์šฑ ๋„์›€์ด ๋˜์‹ค ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค ]

 

 

 

- @GetMapping๊ณผ @PostMapping ์ถœ์ฒ˜

 

[Spring] Spring Framework์—์„œ์˜ @PostMapping๊ณผ @GetMapping: REST API์˜ ํ•ต์‹ฌ ์š”์†Œ

์•ˆ๋…•ํ•˜์„ธ์š”! ์ด๋ฒˆ์—๋Š” Spring Framework์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ธ @PostMapping๊ณผ @GetMapping์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์–ด๋…ธํ…Œ์ด์…˜๋“ค์€ REST API ๊ฐœ๋ฐœ์—์„œ ํ•ต์‹ฌ ์š”์†Œ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์™€ ์„œ

jjangadadcodingdiary.tistory.com

 

+ Recent posts