GET API




GetMapping๊ณผ RequestMapping์„ ํ†ตํ•ด ์ฃผ์†Œ ํ• ๋‹นํ•˜๊ธฐ


package com.example.hello.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/get")
public class GetApiController {

    @GetMapping(path = "/hello")    
    // http://localhost:9090/api/get/hello

    public String hello(){

        return "HELLO";
    }

    @RequestMapping(path = "/hi", method = RequestMethod.GET)    
    // http://localhost:9090/api/get/hi

    // @RequestMapping("/hi")
    // ๊ทธ๋ƒฅ ์œ„์ฒ˜๋Ÿผ๋งŒ ์ ์œผ๋ฉด get / post / put / delete ๋ชจ๋“  ๋ฉ”์„œ๋“œ ๋‹ค ๋งคํ•‘๋จ
    public String hi(){

        return "HI";
    }
}



Path Variable


// http://localhost:9090/api/get/path-variable/{name}
// http://localhost:9090/api/get/path-variable/java
// http://localhost:9090/api/get/path-variable/spring
// {} ์•ˆ์— ๊ณ„์† ๋ณ€ํ•˜๋Š” ๊ฐ’

    @GetMapping("/path-variable/{name}")
    public String pathVariable(@PathVariable String name){
        System.out.println("PathVariable : " + name);
        return name;
    }



// ์ฃผ์†Œ์— name์ด๋ผ๊ณ  ํ•ด ๋†จ๋Š”๋ฐ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋‹ค๋ฅธ๊ฑธ ํ•ด์•ผํ•œ๋‹ค๋ฉด? (์ฃผ์†Œ ์ด๋ฆ„๊ณผ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜ ์—†์„ ๋•Œ)

   @GetMapping("/path-variable/{name}")
   public String pathVariable(@PathVariable(name = "name") String pathName){
       System.out.println("PathVariable : " + pathName);
       return pathName;
   }



Query Parameter


  • ex) ๊ตฌ๊ธ€์— springboot ๊ฒ€์ƒ‰ํ–ˆ์„ ๋•Œ

    https://www.google.com/search
    ?q=springboot
    &oq=springboot
    &aqs=chrome..69i57j0i10i131i433i512j0i512l2j0i10i512j69i60l3.1438j0j7
    &sourceid=chrome
    &ie=UTF-8

    • and ์™€ = ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค! -> key, value

    • ?key=value1&key2=value2

      // http://localhost:9090/api/get/query-param?user=jinsol&email=ejins0193@gmail.com
    
          @GetMapping(path = "query-param")
          public String queryParam(@RequestParam Map<String, String> queryParam){
    
              StringBuilder sb = new StringBuilder();
    
              queryParam.entrySet().forEach(entry -> {
                  System.out.println(entry.getKey());
                  System.out.println(entry.getValue());
    
                  sb.append("\n" + entry.getKey() + "=" + entry.getValue());
              });
    
              return sb.toString();
          }
    




  • QueryParameter์— ๋“ค์–ด๊ฐˆ ๋ณ€์ˆ˜๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•˜๊ธฐ

      // http://localhost:9090/api/get/query-param02?name=jinsol&email=ejins0193@gmail.com
          @GetMapping("query-param02")
          public String queryParam02(
                  @RequestParam String name,
                  @RequestParam String email
          ){
              System.out.println(name);
              System.out.println(email);
    
              return name + ", " + email;
          }
    
    • ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๊ฐ€ ์ƒ๊ธธ ๋•Œ๋งˆ๋‹ค @RequestParam String/int โ€ฆ ํ•ด์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€ > DTO ํ˜•ํƒœ๋กœ ๋งคํ•‘




  • DTO ํ˜•ํƒœ๋กœ ๋งคํ•‘

  • ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฟผ๋ฆฌํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ฐ”๋กœ ๋งคํ•‘์ด ๋˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ!

  • ex)

    • DTO ํด๋” ์ƒ์„ฑ > UserRequest.java

        package com.example.hello.dto;
      
        public class UserRequest {
      
            private String name;
            private String email;
      
            public String getName() {
                return name;
            }
      
            public void setName(String name) {
                this.name = name;
            }
      
            public String getEmail() {
                return email;
            }
      
            public void setEmail(String email) {
                this.email = email;
            }
      
            @Override
            public String toString() {
                return "UserRequest{" +
                        "name='" + name + '\'' +
                        ", email='" + email + '\'' +
                        '}';
            }
        }
      
    • GetApiController.java

      @GetMapping("query-param03")
      
          public String queryParam03(UserRequest userRequest){
      
              System.out.println(userRequest.getName());
              System.out.println(userRequest.getEmail());
      
              return userRequest.toString();
          }
      

Categories:

Spring