[Spring] GET / POST

์‹œ๋‚ดยท2024๋…„ 1์›” 19์ผ

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
5/10

๐Ÿต Annotation

  • ์ž๋ฐ” ์ฝ”๋“œ์— ์ถ”๊ฐ€๋˜๋Š” ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ (๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ)
  • Reflection, proxy ๋“ฑ์˜ ๊ธฐ์ˆ ๋กœ annotation ๊ฐ€๋Šฅ .?

๐ŸŒ @Controller

  • http ํ†ต์‹ ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๋Š” annotation
  • ์‚ฌ์šฉ์ž์˜ ์ตœ์ ‘์ ์—์„œ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๊ณ  ์‘๋‹ต์„ ์ฃผ๋Š” ์—ญํ• 

๐ŸŒ @RestController

  • ๋ชจ๋“  ์š”์ฒญ์— @RequestBody๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” annotation

๐ŸŒ @RequestMapping

  • ํด๋ž˜์Šค ์ฐจ์›์—์„œ url mappingํ•ด์ฃผ๋Š” annotation

๐ŸŒ @RequestMapping

  • json์œผ๋กœ POST ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ, body์—์„œ data๋ฅผ ๊บผ๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” annotation

๐ŸŒ @GetMapping

  • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌํ•˜๋Š” annotation

๐ŸŒ @ResponseBody

  • data๋งŒ returnํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” annotation

๐Ÿต [GET] Examples

  • ์ผ๋ฐ˜์ ์ธ ์กฐํšŒ ์š”์ฒญ์˜ ๊ฒฝ์šฐ

ex) [GET] String ๋ฆฌํ„ด :

    @GetMapping("string")
//    @RequestMapping(value = "string", method = RequestMethod.GET) 
//    ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!
    @ResponseBody
    public String helloString() {
        return "hello_string";
    }

ex) [GET] json ๋ฆฌํ„ด :

	@GetMapping("json")
    @ResponseBody
    public Hello helloJson() {
        Hello hello = new Hello();
        hello.setName("example1");
        hello.setEmail("example1@naver.com");
        hello.setPassword("1234");
        return hello;
    }

ex) [GET] .html ํ™”๋ฉด ๋ฆฌํ„ด :

  • templates ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์— ์œ„์น˜ํ•œ .html ํŒŒ์ผ์„ return
	@GetMapping("screen")
    public String helloScreen() {
        return "screen";
    }

๐Ÿต ์‚ฌ์šฉ์ž๊ฐ€ ๋„˜๊ธด ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ

๐ŸŒ @RequestParam

  • parameter ๋ฐฉ์‹

  • ?{key}={value}

  • ex) localhost:8080/member/?id=1

๐ŸŒ @PathVariable

  • path variable ๋ฐฉ์‹

  • url์„ ํ†ตํ•ด ์ž์›์˜ ๊ตฌ์กฐ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” RESTful API ๋””์ž์ธ์— ์ ํ•ฉ

  • ex) localhost:8080/member/1

๐ŸŒ ex) [GET] ์‚ฌ์šฉ์ž๊ฐ€ ๋„˜๊ธด ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ - parameter ๋ฐฉ์‹

	@GetMapping("param")
    public String helloScreenModelParam(@RequestParam(value = "name") String inputName, Model model) {
//        ํ™”๋ฉด์— data๋ฅผ ๋„˜๊ธฐ๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” model๊ฐ์ฒด ์‚ฌ์šฉ
//        model์— key:valueํ˜•์‹์œผ๋กœ ์ „๋‹ฌ
        model.addAttribute("myData", inputName);
        return "screen";
    }

=> ex) http://localhost:8080/param/?name=example1๋กœ ํ˜ธ์ถœ

๐ŸŒ ex) [GET] ์‚ฌ์šฉ์ž๊ฐ€ ๋„˜๊ธด ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ - path variable ๋ฐฉ์‹

	@GetMapping("pathvariable/{id}")
    public String helloScreenModelPath(@PathVariable int id, Model model) {
        model.addAttribute("myData", id);
        return "screen";
    }

=> ex) http://localhost:8080/pathvariable/1๋กœ ํ˜ธ์ถœ

๐Ÿต [POST] Examples

  • request์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์˜ค๋Š” POST ์š”์ฒญ ๋ฐฉ์‹

๐ŸŒ <form> ํƒœ๊ทธ๋กœ x-www ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ

  • @RequestParam ์‚ฌ์šฉ
  • <form> ํƒœ๊ทธ๋ฅผ ํ†ตํ•œ body ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๊ฐ€ {key1}={value1}&{key2}={value2}
  • Content-Type: application/x-www-form-urlencoded
    -> form ํƒœ๊ทธ ~= url ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹
    cf) Content-Type: multipart/form-data
    -> ์ด๋ฏธ์ง€ ํŒŒ์ผ ํฌํ•จํ•  ๊ฒฝ์šฐ

๐ŸŒ ex) Method 1

	@PostMapping("/form-post-handle1")
    @ResponseBody
    public String formPostHandle1(@RequestParam(value = "name") String name,
                                  @RequestParam(value = "email") String email,
                                  @RequestParam(value = "password") String password) {
        return "์ •์ƒ์ฒ˜๋ฆฌ";
    }

๐ŸŒ ex) Method 2

    @PostMapping("/form-post-handle2")
    @ResponseBody
//    Data binding 
//    Spring์—์„œ Hello ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž๋™ mappingํ•ด์„œ ์ƒ์„ฑ
//    x-www-url ์ธ์ฝ”๋”ฉ ํ˜•์‹์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ
    public String formPostHandle2(Hello hello) {
        System.out.println(hello);
        return "์ •์ƒ์ฒ˜๋ฆฌ";
    }

๐ŸŒ json ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ

  • ajax, axios ํ™œ์šฉ
  • @RequestBody ์‚ฌ์šฉ (HTTP ์š”์ฒญ ๋ณธ๋ฌธ์— ๋‹ด๊ธด ๋ฐ์ดํ„ฐ ์ค‘ ์ฃผ๋กœ JSON์„ Java ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ)
  • ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๊ฐ€ JSON
  • Content-Type: application/json

๐ŸŒ ex) Method 1

  	 @PostMapping("/json-post-handle1")
     @ResponseBody

	 public String jsonPostHandle1(@RequestBody Map<String, String> body) {
        Hello hello = new Hello();
        hello.setName((body.get("name")));
        hello.setEmail((body.get("email")));
        hello.setPassword((body.get("password")));
        return "ok";
    }

๐ŸŒ ex) Method 2

  	@PostMapping("/json-post-handle2")
    @ResponseBody
    
    public String jsonPostHandle2(@RequestBody JsonNode body) {
        Hello hello = new Hello();
        hello.setName((body.get("name").asText()));
        hello.setEmail((body.get("email").asText()));
        hello.setPassword((body.get("password").asText()));
        return "ok";
    }
    }

๐ŸŒ ex) Method 3

  	  @PostMapping("/json-post-handle3")
      @ResponseBody
      
      public String jsonPostHandle3(@RequestBody Hello hello) {
        System.out.println(hello);
        System.out.println(hello.getName());
        System.out.println(hello.getEmail());
        System.out.println(hello.getPassword());
        return "ok";
    }
profile
contact ๐Ÿ“จ ksw08215@gmail.com

0๊ฐœ์˜ ๋Œ“๊ธ€