Home > Back-end >  Pass object of request with GET mapper
Pass object of request with GET mapper

Time:10-09

@GetMapping("/{username}/all")
public ResponseEntity<ProductListResponse> getAllByUsername(PageableRequest pageableRequest, @PathVariable String username){
    return ResponseEntity.ok(productFacade.getAllByUsername(username,pageableRequest));
}

How can I read the PageableRequest in GetMapping

@Getter
@Setter
public class PageableRequest {
    @Builder.Default
    private int pageSize = 15;

    @Builder.Default
    private int pageNumber = 0;

    @Builder.Default
    private SortRequest sort = SortRequest.DESC;
}

CodePudding user response:

Pass parameter no json body http. Parameter pass a PathVariable. I think the best option on RequestParam, on url It be best option to solution the problem.

CodePudding user response:

Sorry, what I meant is to use @ModelAttribute

@GetMapping("/{username}/all")
fun ResponseEntity<ProductListResponse> getAllByUsername(
@ModelAttribute PageableRequest pageableRequest, 
@PathVariable String username
){
    return ResponseEntity.ok(productFacade.getAllByUsername(username,pageableRequest));
}

Also what @Sotirios Delimanolis mentioned, if no annotation is set, @ModelAttribute is default one.

Following example is working for me:

@Operation
@GetMapping
@SecurityRequirement(name = SwaggerConfig.AUTHORIZATION)
public ResponseEntity<List<UserResponse>> getUsers(
        @PageableDefault(sort = [GetUsersSortParameters.LAST_NAME, GetUsersSortParameters.FIRST_NAME])
        paginationParameters: Pageable,
        requestParameters: GetUsersRequestParameters,
        user: Principal
    )
...

The only what I can think of is that you are missing JSON mapping configuration or something related to that.

I have following for Jackson Json objectmapper, this is Kotlin code but it is similar for Java:

@Configuration
class ObjectMapperConfig @Autowired constructor(private val objectMapper: ObjectMapper) {
    @PostConstruct
    fun configure() {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    }
}
  • Related