GET API
08 Oct 2021 -
1 minute read
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(); }
-