
@RestController이다 RequestMapping이다@Slf4j
@RestController
@RequestMapping("/api")
public class RestApiController {
@GetMapping(path = "/hello")
public String hello(){
return "Hello Spring Boot";
}
@RestController로 컨트롤러를 설정하였고, @RequestMaping을 클래스 레벨로 설정해, 하위 메서드들이 모두 /api 라는 주소를 받는다는 것을 알 수 있다@GetMapping으로 RequestMapping을 Get메서드로 받는 다는 것을 알 수 있고


여기서 우리는 응답은 결국 문자열이라는 것을 알 수 있다
- 그것을 예쁘게 꾸미는 것이다
- 결국 0,1들의 비트 단위의 데이터를 html, xml, json등으로 리소스를 표현하는 것이다
- ✅ 결국 통신이란 문자를 전달하는 것!
@PathVariable 어노테이션을 이용해서 주소 내에 정보를 전달할 수 있다public String echo(
@PathVariable("message") String message
){
log.info("echo message : "+message);
return message;
}
🤔 쿼리 파라미터에서 Wrapper vs primative 타입
- Integer의 Wraaper 타입은 null값이 들어올 수 있다
- 하지만 primative 타입인 Int는 null 값이 들어올 수 없다
- 👌 내 생각에는, 쿼리 파라미터에 null 값이 들어오면 안되기 때문에, 쿼리 파라미터에서는 Wrapper 타입 보단 primative 타입을
지향해야 한다!- 실제로 null값이 파라미터에 들어가면 404 Error가 발생한다!
@GetMapping(path="/book")
public void queryParam(
@RequestParam(name = "category") String category,
@RequestParam(name="issuedYear") String issuedYear,
@RequestParam(name = "issued-month") String issuedMonth,
@RequestParam(name = "issued_day") String issuedDay
){
log.info(category);
log.info(issuedYear);
log.info(issuedMonth);
log.info(issuedDay);
}
@PathVariable에 매개변수로 이름을 알려줘서 파싱을 해준다

/api/book?category=IT&issuedYear=2023&issued-month=01&issued_day=31
- 다음 주소에 파라미터들을 받는다고 생각해보자
issuedYear: Camel caseissued-month: 하이푼이 들어간 경우issued_day: Snake case
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookQueryParam {
private String category;
private String issuedYear;
private String issuedMonth;
private String issuedDay;
}
@GetMapping(path="/book2")
public void queryParamDto(
BookQueryParam bookQueryParam
){
System.out.println(bookQueryParam);
}
@ModelAttribute를 붙이는데, 생략도 가능하다api/book2?category=IT&issuedYear=2023&issued-month=01&issued_day=31 실행 
api/book2category=IT&issuedYear=2023&issuedMonth=01&issuedDay=31 실행
✅ 객체로 받는 방법 vs RequestParam으로 하나씩 받는 방법
- 객체로 받는 방법은, 변수의 내용이 많을 때 사용하면 유용하다
- 같이 협력하는 규칙을 주소에 Camel case가 아닌 하이푼 방식으로 하자라고 정한 것과 같이, 객체에 바로들어가지 않는다고 예상될 때는 @RequestParam을 쓸 수 있다
- 협력하는 개발자와 잘 정하고, 편의사항에 맞춰서 파싱을 해주자!


netstat -ano | findstr 8080
taskkill /f /pid 27852

지금까지 get방식으로 데이터를 받을 때는, 특별한 객체 없이 @RequestParam을 통해 쿼리 파라미터를 받을 수 있었다
@RequestBody 라는 어노테이션을 사용하는데 PUT, POST 방식에서 Http Body로 들어오는 데이터를 해당 객체에다가 매핑을 해주는 역할을 한다@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookRequest {
private String name;
private String number;
private String category;
}
@Slf4j
@RestController
@RequestMapping(path = "/api")
public class PostApiController {
@PostMapping("/post")
// http://localhost:8080/api/post
public BookRequest post(
@RequestBody BookRequest bookRequest
){
log.info(bookRequest.toString());
return bookRequest;
}
}


application/json으로 json 타입의 데이터가 Body에 실려오는 것을 알 수 있다🤔 만약 return 타입을 String으로 보낸다면...?

text/plain;charset=UTF-8로 오는 것을 확인할 수 있다!{
"key" : "value",
"array" : [
10,
20,
30
],
"string_array" : [
"홍길동","이순신","유관순"
],
"object_array" : [
{
"name" : "홍길동"
},
{
"name" : "이순신"
},
{
"name" : "유관순"
}
]
}

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
🤔 Entity,Dto에서 Wrapper vs primative 타입
- 앞단에서 쿼리 파라미터에서는 null을 받으면 404 에러가 나므로, primative타입을 사용하자고 했다
- Entity,Dto에서는 Wrapper 클래스를 써주자!
- primative 타입은 null이면 0을 넣기 때문에, 🤔값이 들어가지 않았는데 들어간 것 처럼 볼 수 있다
- null을 허용하는 Wrapper클래스로 Entity나 Dto에 사용하자!
@Slf4j
@RestController
@RequestMapping(path = "/api")
public class PutApiController {
@PutMapping("/put")
public void put(
@RequestBody
UserRequest userRequest
){
log.info("Request : {}", userRequest);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) // 헤당 클래스의 변수들을 snake_case로 매핑하겠다!
public class UserRequest {
private String userName;
private String userAge;
private String email;
private boolean isKorean;
}
🤔boolean? Boolean?
- 원시형과 참조형 뭐를 쓸까? -> 이번엔 기본값을 false로 지정하기 위해서 원시형을 썼다
- 그런데...
- primitive type에서 is 시리즈는 boolean을 듯하기 때문에 setIsKorean이 아닌 SetKorean으로 만들어진다
- 다음을 요청해도, 우리는 false를 받게 된다
- 해결방법 1. korean으로 요청한다
- 하지만 이는, json에 우리가 전송하는 파라미터의 의미를 제대로 전달하지 못한다
- 해결방법 2. wrapper 클래스인 Boolean으로 바꾼다
- wrapper 클래스로 바꾸자 잘 들어간 것을 볼 수 있다!
@DeleteMapping(path = {
"/user/{userName}/delete",
"/user/{userName}/del"}) // path로 명시 하면 {}안에 여러 주소를 넣어줄 수 있다
public void delete(
@PathVariable("userName") String userName
){
log.info("user-name : {}",userName);
}
path =?
- RequestMapping 시리즈에, controller가 담당할 주소를 적어주었다
- 이때 path 라는 파라미터를 사용하면 -> 여러개의 주소를 {}안에 적어줄 수 있다