[Springboot] RESTful Get해보기
@욕심쟁이
·2021. 11. 5. 23:40
반응형
- Get은 Pathvariable, QueryParameter 사용가능
1. src > main > java > 패키지 > controller 패키지 생성 후 GetController.class 생성
2. src > main > java > 패키지 > dto 생성 후 UserDto 생성
3. DTO(UserRequest)
package com.example.springboot202111.dto;
public class UserRequest {
private String user;
private String email;
private int age;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserRequest{" +
"user='" + user + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
3. @GetMapping
package com.example.springboot202111.controller;
import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.springboot202111.dto.UserRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController // RESTAPIController 사용한다는 것을 알려줌
@RequestMapping("/api/get") // localhost:8080/api/get 매핑
public class GetController {
//가장 많이 사용
//http://localhost:8080/api/get/queryParam3?user=jiyeon&email=jiyoen@naver.com&age=20
@GetMapping(path = "/queryParam3")
public String queryParam3(UserRequest userRequest){
// userDTO에서 UserRequest userRequest 값가져옴
System.out.println(userRequest.getUser());
System.out.println(userRequest.getEmail());
System.out.println(userRequest.getAge());
return userRequest.toString();
};
//@RequestMapping("/hi") // get/ post / put / delete 모든 RESTful 동작
//@RequestMapping(path = "/hi", method = RequestMethod.GET) // 메서드 설정시 get http//localhost:8080/api/get/hi
//@GetMapping(path = "getHello") : 명시적
@GetMapping("/getHello") // http://localhost:8080/api/get/getHello
public String getHello(){
return "get Hello";
};
//변화하는 부분은 @PathVariable을 사용
//public String pathVariale(@PathVariable String name){
// @PathVariable(name = "name") name 변수명을 다르게 설정
@GetMapping("/pathVariale/{name}") // ://localhost:8080/api/get/pathVariale/내가 입력한 path
public String pathVariale(@PathVariable(name = "name") String pathName){
System.out.println("PathVariable : " + pathName );
return pathName;
};
//http://localhost:8080/api/get/queryParam?user=jiyeon&email=jiyoen@naver.com&age=20
@GetMapping(path = "/queryParam")
public String queryParam(@RequestParam Map<String, String > queryParams){
StringBuilder sb = new StringBuilder();
queryParams.entrySet().forEach(val -> {
System.out.println(val.getKey());
System.out.println(val.getValue());
System.out.println("\n");
sb.append(val.getKey() +" = "+ val.getValue());
});
return sb.toString();
};
//http://localhost:8080/api/get/queryParam2?user=jiyeon&email=jiyoen@naver.com&age=20
@GetMapping(path = "/queryParam2")
public String queryParam2(
@RequestParam String user,
@RequestParam String email,
@RequestParam int age
){
System.out.println(user);
System.out.println(email);
System.out.println(age);
return user +" "+ email +" "+ age;
};
}
참고. 패스트캠퍼스 Springboot강의
반응형