요즘에는 NestJS에서 3tier Layer가 아니라 CQRS를 사용해서 DDD를 진행하고 있는데 제대로 해본 적이 없어서 그런지 고민할게 너무 많다. 비즈니스 로직을 제대로 구분해놓은건지 맞는건지 의문이다.
@RestController
@RequestMapping("/coffee")
class CoffeeController(private val coffeeRepository: CoffeeRepository) {
@GetMapping
fun getCoffeeList(): Mono<ResponseEntity<RestResponseDto<List<Coffee>>>> {
return coffeeRepository.findAll().collectList().map {
ResponseEntity.ok().header("asdf", "wer").body(RestResponseDto(it))
}
}
@GetMapping("/{id}")
fun getCoffeeById(@PathVariable id: String): Mono<ResponseEntity<Coffee>> {
return coffeeRepository.findById(id).map { ResponseEntity.ok().body(it) }
}
@PostMapping
fun postCoffee(@RequestBody coffee: Coffee): Mono<Coffee> {
return coffeeRepository.save(coffee)
}
@PutMapping("/{id}")
fun putCoffee(@PathVariable id: String, @RequestBody coffee: Coffee): Mono<Coffee> {
return coffeeRepository.save(coffee)
}
@DeleteMapping("/{id}")
fun deleteCoffee(@PathVariable id: String): Unit {
coffeeRepository.deleteById(id)
}
}
Flux 예전에 안드로이드 할 때 들었던 개념인데 Spring에서도 사용하는구나. 그래도 안드로이드로 실무를 있어서 그런지 Spring과 Kotlin의 이해가 잘되는 편인 것 같다!
이것 저것 검색해보니까 Coroutine도 사용하는 것 같다. 나중에 찾아봐야겠다.
잡담이 길었고 Mono는 단일값 Flux는 iterable한 값이라고 생각된다. RxJS에서 firstValueFrom()/lastValueFrom()은 Mono로 대응되고 Flux는 Observable<T>라고 생각이 된다.
http://localhost:8080/coffee 응답
[
{
"id": "679c20a2-b256-433d-b42a-b75db31094aa",
"name": "Espresso"
},
{
"id": "c0988bcf-8367-490c-81b4-f849c3bdcc9a",
"name": "Cafe Latte"
}
]
flux는 subscribe 하고 있다가 마지막 데이터를 내려주는 것으로 보인다
ResponseEntity를 사용해서 진행하면
{
"body": [
{
"id": "42e59bb6-b147-428c-a761-3ca7af14e6b4",
"name": "Espresso"
},
{
"id": "6817c180-3ef8-43d9-b252-58cbb3ec5260",
"name": "Cafe Latte"
}
]
}
ResponseEntity의 ok() , header()이런게 하나도 안 먹는다!
findAll()는 호출하면 잘되지만 findById()를 호출하면
Required identifier property not found for class daljin.spring.study.Coffee
이따구로 에러나면서 안된다. 하.. R2DBC가 문제인 것 같긴한데 @Id 어노테이션을
@org.springframework.data.annotation.Id 이거로 바꿔서 하라고 하는데 이것도 안된다.
뭘해도 안된다.. 하 ㅠㅠㅠㅠ 월요일에 출근하면 자바 개발자분들에게 쫄래쫄래 가서 여쭤봐야겠다
JPA로 테스트할때는 뭘해도 잘되는데 R2DBC와 WebFlux사용하려니까 뭘해도 다 안된다. 왜이래
물론 내가 사용할 줄 몰라서겠지만 안드로이드 개발 할 때 Suspend 사용하는 것도 문제 없었고
NodeJS 처음 개발할 때 Promise도 문제 없었는데 얘는 왜이럴까.. 에러 나와서 검색해봐도 거의 나오는게 없다 ㅠㅠㅠ Webflux를 사용하는 곳이 없는걸까 :(
[오늘의 결론]
Webflux + R2DBC가 생각대로 움직이지 않는다. 스프링부트 공부 하면서 처음으로 노드가 편하게 느껴졌다.