MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[spring boot] RestController

HYEJU01 2024. 9. 5. 23:42
  • @RestController //Controller + ResponseBody(์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์‘๋‹ต์„ ์š”์ฒญ์ด ๋“ค์–ด์˜จ๊ณณ์œผ๋กœ ๋ฐ”๊ฟˆ) ํ•ฉ์„ฑ์–ด

 

  •  

 

 

JSON ํ˜•์‹์˜ ๋ฐ˜ํ™˜

 

1. Vo ํƒ€์ž…

 

 

2. Map ํƒ€์ž…

 

 

 

 

 

jsonํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก
@RequestBody - JSON๋ฐ์ดํ„ฐ๋ฅผ -> ์ž๋ฐ” ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€ํ˜•ํ•ด์„œ ๋งตํ•‘
{ "name" : "ํ™๊ธธ๋™", "age" : 20, "addr" : "์„œ์šธ์‹œ์‹œ" }

 

 

 

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
        $("#getBtn").click(function() {

            $.ajax({
                type : "get", //์š”์ฒญ๋ฐฉ์‹
                url : "/getData2/1/์ด์ˆœ์‹ ", //์š”์ฒญ์ฃผ์†Œ
                success : function(data) {
                    console.log(data)
                },
                error: function(err, status) {
                    console.log(err)
                }
            })

        })

        $("#postBtn").click(function() {
            $.ajax({
                type : "post", //์š”์ฒญ๋ฐฉ์‹
                url : "/getEntity", //์š”์ฒญ๋ฐฉ์‹
                data : JSON.stringify({num : 1, name : "ํ™์ˆœ์ž"}), //๋ณด๋‚ผ๋ฐ์ดํ„ฐ
                contentType : "application/json", //๋ณด๋‚ผ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํƒ€์ž…
                dataType : "json", //์„œ๋ฒ„๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž… (์ƒ๋žต๊ฐ€๋Šฅ)
                success : function(data) {
                    console.log(data);
                },
                error : function(err, status) {
                    console.log(err);
                }
            })
        })



    </script>

 

 

    ///////////////////////////////////////////////////////////////////////////////
    //์‘๋‹ต๋ฌธ์„œ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ ResponseEntity<๋ฐ์ดํ„ฐํƒ€์ž…>
    //@CrossOrigin({"http://127.0.0.1:5500", "http://localhost:5500"})
    @CrossOrigin("*") //๋ชจ๋“  ์„œ๋ฒ„์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์Šน์ธํ•จ(์œ„ํ—˜ํ•  ์ˆ˜ ์žˆ์Œ)
    @PostMapping("/getEntity")
    public ResponseEntity<TestVO> getEntity( @RequestBody TestVO v) {

        System.out.println("๋ฐ›์€ ๋ฐ์ดํ„ฐ:" + v.toString());

        TestVO vo = new TestVO(1, "ํ™๊ธธ๋™", 20, "์„œ์šธ์‹œ");

        //1st
        //ResponseEntity entity = new ResponseEntity(HttpStatus.BAD_REQUEST);
        //ResponseEntity entity = new ResponseEntity(vo, HttpStatus.BAD_REQUEST);

        //2nd
        HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "Bearer JSON WEB TOKEN~" ); //ํ‚ค, ๊ฐ’
        header.add("Content-Type", "application/json"); //produce์™€ ๊ฐ™์€ ํ‘œํ˜„
        //header.add("Access-Control-Allow-Origin", "http://example.com");

        ResponseEntity entity = new ResponseEntity(vo, header, HttpStatus.OK); //๋ฐ์ดํ„ฐ, ํ—ค๋”, ์ƒํƒœ๊ฐ’
        return entity;
    }
    ///////////////////////////////////////////////////////////////
    /*
    ์š”์ฒญ์ฃผ์†Œ : /api/v1/getData
    ๋ฉ”์„œ๋“œ : get
    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ : sno(์ˆซ์ž), name(๋ฌธ์ž)
    ์‘๋‹ต ํŒŒ๋ผ๋ฏธํ„ฐ : MemoVO
    ํ—ค๋”์— ๋‹ด์„ ๋‚ด์šฉ HttpStatus.OK
    fetch API ์‚ฌ์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ์š”์ฒญ ์‘๋‹ต
    */

    @GetMapping("/api/v1/getData")
    public ResponseEntity<MemoVO> exampleData(@RequestParam("sno") int sno,
                                          @RequestParam("name") String name) {
        System.out.println(sno + ", " + name);

        return new ResponseEntity<MemoVO>(
                                 new MemoVO(1L, "ํ™๊ธธ๋™", "1234", null, "Y")
                                , HttpStatus.OK);
    }
    /*
    ์š”์ฒญ์ฃผ์†Œ : /api/v1/getInfo
    ๋ฉ”์„œ๋“œ : post
    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ : MemoVOํƒ€์ž…
    ์‘๋‹ต ํŒŒ๋ผ๋ฏธํ„ฐ : List<MemoVO>ํƒ€์ž…
    ํ—ค๋”์— ๋‹ด์„ ๋‚ด์šฉ HttpStatus.OK
    fetch API ์‚ฌ์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ์š”์ฒญ ์‘๋‹ต
    */
    @PostMapping("/api/v1/getInfo")
    public ResponseEntity<List<MemoVO>> getInfo(@RequestBody @Valid MemoVO vo,
                                                BindingResult binding) {
        if(binding.hasErrors()) {
            System.out.println("์œ ํšจ์„ฑ ๊ฒ€์ฆ์— ์‹คํŒจํ•จ");
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
        //DB....
        List<MemoVO> list = new ArrayList<>();
        list.add(vo);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

 

 

 

 

 

 

@RestController //Controller + ResponseBody(์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์‘๋‹ต์„ ์š”์ฒญ์ด ๋“ค์–ด์˜จ๊ณณ์œผ๋กœ ๋ฐ”๊ฟˆ) ํ•ฉ์„ฑ์–ด
public class RestBasicController {

    @GetMapping("/hello")
    public String hello() {
        return "์ด๊ฒŒ ๋ฌด์•ผ??"; //์š”์ฒญ์„ ๋ณด๋‚ธ๊ณณ์œผ๋กœ ์‘๋‹ตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    }
    
    @GetMapping("/hello2")
    public String[] hello2() {
        return new String[] {"ํ™", "๊ธธ", "๋™"}; //์š”์ฒญ์„ ๋ณด๋‚ธ๊ณณ์œผ๋กœ ์‘๋‹ตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    }

    ////////////////////////////////////////////////////////////////////////
    //get๋ฐฉ์‹ ์š”์ฒญ๋ฐ›๊ธฐ - ์ผ๋ฐ˜์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ›๋Š”ํ˜•์‹๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ€๋Šฅํ•จ
    //http://localhost:8181/getData?num=1&name=ํ™๊ธธ๋™

    //1st
//    @GetMapping("/getData")
//    public String getData(TestVO vo) {
//        System.out.println(vo.toString());
//        return "getData";
//    }

    @GetMapping("/getData")
    public String getData(@RequestParam("num") int num,
                          @RequestParam("name") String name) {
        System.out.println(num + ", " + name );
        return "getData";
    }

    //ํŒจ์Šค๋ฒ ๋ฆฌ์–ด๋ธ” ๋ฐฉ์‹
    //http://localhost:8181/getData2/1/ํ™๊ธธ๋™
    @GetMapping("/getData2/{num}/{name}")
    public String getData2(@PathVariable("num") int num,
                           @PathVariable("name") String name) {
        System.out.println(num + "," + name);
        return "success";
    }

    //๋ฐ˜ํ™˜์„ JSONํ˜•์‹์œผ๋กœ ํ•˜๋ ค๋ฉด Mapํƒ€์ž…์ด๋‚˜ VO๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. (list, ๋ฐฐ์—ด๋„ ๋จ)
    //Jackson-databind๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•จ(์Šคํ”„๋ง๋ถ€ํŠธ์— ๊ธฐ๋ณธ ํฌํ•จ๋จ)

    //1st
//    @GetMapping("/returnData")
//    public TestVO returnData() {
//        return new TestVO(1, "์„œ๋ฒ„์—์„œ๋ฐ˜ํ™˜", 20, "์„œ์šธ์‹œ");
//    }
    //2nd
    @GetMapping("/returnData")
    public Map<String, Object> returnData() {
        Map<String, Object> map = new HashMap<>();
        map.put("num", 1);
        map.put("name", "ํ™๊ธธ๋™");
        map.put("arr", Arrays.asList("a", "b", "c"));
        return map;
    }

    ///////////////////////////////////////////////////////////////////
    //post๋ฐฉ์‹ - ์†Œ๋น„์ž(์‚ฌ์šฉ์ž) ์™€ ์ œ๊ณต์ž(์„œ๋ฒ„) ์ด ๋‘˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ทœ์•ฝ์ด ์ •ํ™•ํ•˜๊ฒŒ ์ง€์ผœ์ ธ์•ผ ํ•จ

    //formํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก - ์†Œ๋น„์ž ๋ฐ์ดํ„ฐ๋ฅผ Formํ˜•์‹์œผ๋กœ ๋ฐ˜๋“œ์‹œ ๋งŒ๋“ค์–ด์„œ ๋ณด๋‚ด์•ผ ํ•จ
    //http://localhost:8181/getForm
    @PostMapping("/getForm")
    public String getForm( TestVO vo ) {
        System.out.println(vo.toString());
        return "success";
    }
    
    //jsonํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก
    //@RequestBody - JSON๋ฐ์ดํ„ฐ๋ฅผ -> ์ž๋ฐ” ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€ํ˜•ํ•ด์„œ ๋งตํ•‘
    //{ "name" : "ํ™๊ธธ๋™", "age" : 20, "addr" : "์„œ์šธ์‹œ์‹œ" }

    //1st
//    @PostMapping("/getJSON")
//    public String getJSON( @RequestBody TestVO vo) {
//        System.out.println(vo.toString());
//        return "success";
//    }

    @PostMapping("/getJSON")
    public String getJSON( @RequestBody Map<String, Object> map) {
        System.out.println(map.toString());
        return "success";
    }
    //@PutMapping (์ˆ˜์ •), @DeleteMapping (์‚ญ์ œ) - Post๋ฐฉ์‹๊ณผ ๊ฑฐ์˜ ์œ ์‚ฌํ•จ

    ///////////////////////////////////////////////////////////////////////////////
    //consumer - ๋ฐ˜๋“œ์‹œ ์ดํƒ€์ž…์œผ๋กœ ๋ณด๋‚ด๋ผ!! ์•ˆ๊ทธ๋Ÿผ ์ฃฝ๋Š”๋‹ค.
    //producer - ๋‚ด๊ฐ€ ์ดํƒ€์ž…์œผ๋กœ ์ค„๊ฒŒ!!
    //๊ธฐ๋ณธ๊ฐ’์€ - "application/json" ์ž…๋‹ˆ๋‹ค.
    @PostMapping(value = "/getResult", produces = "text/html", consumes = "text/plain")
    public String getResult( @RequestBody String str ) {
        System.out.println(str);
        return "<h3>๋ฌธ์ž์—ด</h3>";
    }

    ///////////////////////////////////////////////////////////////////////////////
    //์‘๋‹ต๋ฌธ์„œ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ ResponseEntity<๋ฐ์ดํ„ฐํƒ€์ž…>
    //@CrossOrigin({"http://127.0.0.1:5500", "http://localhost:5500"})
    @CrossOrigin("*") //๋ชจ๋“  ์„œ๋ฒ„์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์Šน์ธํ•จ(์œ„ํ—˜ํ•  ์ˆ˜ ์žˆ์Œ)
    @PostMapping("/getEntity")
    public ResponseEntity<TestVO> getEntity( @RequestBody TestVO v) {

        System.out.println("๋ฐ›์€ ๋ฐ์ดํ„ฐ:" + v.toString());

        TestVO vo = new TestVO(1, "ํ™๊ธธ๋™", 20, "์„œ์šธ์‹œ");

        //1st
        //ResponseEntity entity = new ResponseEntity(HttpStatus.BAD_REQUEST);
        //ResponseEntity entity = new ResponseEntity(vo, HttpStatus.BAD_REQUEST);

        //2nd
        HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "Bearer JSON WEB TOKEN~" ); //ํ‚ค, ๊ฐ’
        header.add("Content-Type", "application/json"); //produce์™€ ๊ฐ™์€ ํ‘œํ˜„
        //header.add("Access-Control-Allow-Origin", "http://example.com");

        ResponseEntity entity = new ResponseEntity(vo, header, HttpStatus.OK); //๋ฐ์ดํ„ฐ, ํ—ค๋”, ์ƒํƒœ๊ฐ’
        return entity;
    }
    ///////////////////////////////////////////////////////////////
    /*
    ์š”์ฒญ์ฃผ์†Œ : /api/v1/getData
    ๋ฉ”์„œ๋“œ : get
    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ : sno(์ˆซ์ž), name(๋ฌธ์ž)
    ์‘๋‹ต ํŒŒ๋ผ๋ฏธํ„ฐ : MemoVO
    ํ—ค๋”์— ๋‹ด์„ ๋‚ด์šฉ HttpStatus.OK
    fetch API ์‚ฌ์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ์š”์ฒญ ์‘๋‹ต
    */

    @GetMapping("/api/v1/getData")
    public ResponseEntity<MemoVO> exampleData(@RequestParam("sno") int sno,
                                          @RequestParam("name") String name) {
        System.out.println(sno + ", " + name);

        return new ResponseEntity<MemoVO>(
                                 new MemoVO(1L, "ํ™๊ธธ๋™", "1234", null, "Y")
                                , HttpStatus.OK);
    }
    /*
    ์š”์ฒญ์ฃผ์†Œ : /api/v1/getInfo
    ๋ฉ”์„œ๋“œ : post
    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ : MemoVOํƒ€์ž…
    ์‘๋‹ต ํŒŒ๋ผ๋ฏธํ„ฐ : List<MemoVO>ํƒ€์ž…
    ํ—ค๋”์— ๋‹ด์„ ๋‚ด์šฉ HttpStatus.OK
    fetch API ์‚ฌ์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ์š”์ฒญ ์‘๋‹ต
    */
    @PostMapping("/api/v1/getInfo")
    public ResponseEntity<List<MemoVO>> getInfo(@RequestBody @Valid MemoVO vo,
                                                BindingResult binding) {
        if(binding.hasErrors()) {
            System.out.println("์œ ํšจ์„ฑ ๊ฒ€์ฆ์— ์‹คํŒจํ•จ");
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
        //DB....
        List<MemoVO> list = new ArrayList<>();
        list.add(vo);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

 

 

    <button type="button" id="ex01">ex01</button>
    <button type="button" id="ex02">ex02</button>

    <script>
        var ex01 = document.getElementById("ex01");
        ex01.addEventListener("click", function() {

            fetch("/api/v1/getData?sno=1&name=์ด์ˆœ์‹ ")
                .then(function(response) {
                    return response.json() //์‘๋‹ต json
                })
                .then(function(data) {
                    console.log(data)
                })
        })

        var ex02 = document.getElementById("ex02");
        ex02.addEventListener("click", function() {

            fetch("/api/v1/getInfo", {
                method : "post",
                headers : {
                    "Content-type" : "application/json"
                },
                body : JSON.stringify({memo: "ํ™๊ธธ๋™123123", phone: "010-1234-1234"})
            })
            .then(function(response) {
                return response.json();
            })
            .then(function(data) {
                console.log(data);
            })

        })


    </script>