대머리개발자

아르메리아 End-Point 설정 본문

개발이야기/코틀린

아르메리아 End-Point 설정

대머리개발자 2024. 8. 23. 11:18
728x90

애초에 완벽한 요구사항으로 나왔다면 하지만.... 그럴수... 럴수 없기 때문에 늘 확장성을 고민..해야 한다.

 

뭐 일단 기존 잘 사용하고 있는 기능이고

@Get("/product/{style}/count")
fun findAllAvailableCount(@Param("style")style: String): Mono<ApiResponse>

 

userId의 값이 필요한 케이스가 생겼다.

@Get("/product/{style}/count")
fun findAvailableCount(@Param("style")style: String, @Param("userId")userId: String?): Mono<ApiResponse> {

 

해서 추가 @Param을 userId의 물음표로 처리만 하면 될 것 같았는데

 

기존코드가 안 돌아간다. 아래처럼...

Mandatory parameter/header is missing: userId

 

 

결국 두 개의  End-Point를 생성해야 할 것 같은..

@Get("/product/{style}/count/{userId}")
fun findAllAvailableCount(@Param("style")style: String, @Param("userId")userId: String?): Mono<ApiResponse>

 

나의 목표는 하나의 End-Point로 작성하는 것이다. 

 

아르메리아를 리소를 까 보니께 아래와 같은 친구가 있었다.

@MatchesParam

 

그래서 최종 리소스는

@Get("/product/{style}/count")
@MatchesParam("userId")
fun findAllAvailableCount(@Param("style")style: String, @Param("userId")userId: String): Mono<ApiResponse> {
    return productService.findMyAllAvailableCount(getStyle(style), userId).flatMap {
       ...
    }
}

@Get("/product/{style}/count")
fun findAllAvailableCount(@Param("style")style: String): Mono<ApiResponse> {
    return productService.findMyAllAvailableCount(getStyle(style)).flatMap {
        ...
    }
}

 

EndPoint는 같고 @MatchesParam을 통해서 userId의 유무로!! 갈라치기 하는 것이다.

 

참고로 아르메리아에서는 @Param은 두 가지(@PathVariable, @RequestParam)역할 모두 하나로 처리 한다..

 

쓰다 보니 더 깔끔한 방법을 찾았다.

최최초종 리소스..

@Get("/product/{style}/count")
fun findAllAvailableCount(@Param("style")style: String, queryString: QueryParams): Mono<ApiResponse> {

 

자동으로 QueryParams으로 바인딩 해주네!!! 야호!! 래퍼런스가 없는 것은 얻어 걸려야 해 ㅠ

728x90

'개발이야기 > 코틀린' 카테고리의 다른 글

다시 한번 코루틴 적용해 보자.  (8) 2024.10.09
armeria aop  (6) 2024.09.10
시크릿 컬럼 추가에 대한 말도 안 되는 고민  (0) 2024.08.05
데이터 정규화  (1) 2024.05.17
[코틀린]null 처리 재미지다!  (0) 2024.04.02