0

My PageSort Entity,

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageSort {
    private String sortBy;
    @SortValueConstraint
    private String sortDirection;
}

My ProjectPage Entity

@Data
@RequiredArgsConstructor
@Validated
public class ProjectPage {
    @Min(value = 1, message = "page number must be greater than zero")
    @IsNumberValidatorConstraint(message = "page number must be numeric")
    private String page = "1";

    @Min(value = 1, message = "page size must be greater than zero")
    @IsNumberValidatorConstraint(message = "page size must be numeric")
    private String size = "25";

    @Valid
    private List<PageSort> sort = Arrays.asList(PageSort.builder().sortBy("id").sortDirection("asc").build());
}

My RestController class

@Validated
@RestController
@RequestMapping("/api/projects")
public class ProjectController extends AbstractCRUDController<Project, ProjectPostDTO, ProjectPatchDTO, ProjectResponseDTO> {
    private ProjectApi projectApi;

    @GetMapping
    public ResponseEntity<ApiResponseDTO> getProjects(
            @Valid ProjectPage projectPage,
            @Valid ProjectSearchCriteria projectSearchCriteria){
        return new ResponseEntity<ApiResponseDTO>(null, HttpStatus.OK);
    }
}

I am trying to invoke the getProjects method as below

enter image description here

page and size parameter values are properly fetched inside the ProjectPage object but not the sort list in projectPage. But I tried using a single instance without the List and this worked perfectly.

enter image description here

Found a similar issue posted here Spring validation for RequestBody parameters bound to collections in Controller methods but did not solve the issue.

Any ideas on how to get this sorted?

1 Answer 1

0

RequestParam does not work on complex objects, only Strings. So you can have a List for all sortBy, and List for all sortDirection.

@GetMapping
    public ResponseEntity<ApiPageResponseDTO> getProjects(
            @Valid CustomPage page,
            @Valid @RequestParam(value = "sortBy", required = false) List<String> sortByList,
            @Valid @RequestParam(value = "sortDirection", required = false) @SortValueConstraint List<String> sortDirectionList,
            @Valid ProjectSearchCriteria projectSearchCriteria){
        final List<PageSort> sortList = new ArrayList<>();
        validateSortByAndSortDirectionsList(page, sortByList, sortDirectionList, sortList);
        ApiPageResponseDTO projectResponseDTO = this.projectApi.getProjects(page, projectSearchCriteria);
        return new ResponseEntity<ApiPageResponseDTO>(projectResponseDTO, HttpStatus.OK);
    }

Reference - Spring MVC @RequestParam a list of objects

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.