0

I have the following method:

public MenuExpandedDTO findMenuExp(UUID menuUuid) {
    final MenuDTO menu = menuService.findByUuid(menuUuid);
    final MenuPropertiesDTO propertiesDTO = new MenuPropertiesDTO(
            menu.getUuid(),
            menu.getName()
    );

    final List<GroupExpDTO> groups = menuGroupService
            .findAllByMenuUuid(menuUuid).stream()
            .map(menuGroup -> {
                UUID groupUuid = menuGroup.getGroupUuid();
                return findGroupExp(groupUuid);
            })
            .collect(Collectors.toList());

    return new MenuExpDTO(propertiesDTO, groups, null, null);
}

In this method, I pass a single menuUuid and then get combination of a single MenuPropertiesDTO and List<GroupExpDTO>.

I want to pass a List<menuUuid> instead of a single menuUuid and then get the result according to the uuids in this list. However, I am confused if there is a proper way for this in Java. I think there is no need to use loop and it would be possible to evaluate this using stream. But have really no idea how to construct it or if it is possible. Any help would be appreciated.

geobreze
  • 2,274
  • 1
  • 10
  • 15
  • 4
    You can create new method `public List findMenuExp(List menuUuid) { return menuUuid.stream().map(this::findMenuExp).collect(Collectors.toList()); }` – geobreze Aug 01 '21 at 14:34
  • *get the result according to the uuids in this list* - How should the result type look like? Is it a `Map` or a `List`? – Thiyagu Aug 01 '21 at 14:34
  • @geobreze Thanks a lot for your help, but I am confused about the usage. On the other hand, should I change the group part? What about posting your suggestion as an answer? –  Aug 01 '21 at 18:20
  • @user7 I need to use `List` –  Aug 01 '21 at 18:21

1 Answers1

0

If I understood correctly from the question and comments, you need a method, which will work in the same manner, but for a list of UUIDs.

To achieve this, you can create new method, which will call the one you've implemented.

public List<MenuExpandedDTO> findMenuExp(List<UUID> menuUuids) {
    return menuUuids.stream()
        // assuming this method is added to the same class, I'm referring 'this' here
        .map(this::findMenuExp)
        .collect(Collectors.toList());
}

This method will return of list having size equal to menuUuids.size() and ith element of List<MenuExpandedDTO> will correspond to ith element of menuUuids list.

geobreze
  • 2,274
  • 1
  • 10
  • 15
  • Thanks a lot for your useful helps. I tried to apply this approach and I think it is a smarter solution. However, I have a little problem. Normally I pass a **selected** uuid to the Controller via `@PathVariable UUID menuUuid` in the Controller. But, I have no idea how I should pass multiple selected uuid to the Controller. Assume that I pass `List menuUuid` to the Controller. Should I pass them as method parameter instead of `@PathVariable`? –  Aug 01 '21 at 20:15
  • @Henry, you can pass list even to `@PathVariable`. https://stackoverflow.com/a/22298768/4655217. But if it's possible, I would recommend making it a query parameter for more clarity, since you can name you variable in pluar form explicitly saying that you're expecting list of ids. – geobreze Aug 01 '21 at 20:21
  • I tried `@RequestParam List menuUuid`, but it gives *"menuUuid parameter is missing"* error. I pass the value in Postman like `{ "menuItemUuid": ["6849bcbb-c26c-4299-9ca4-dcde56830f4e"] }`. So, should I use query parameter instead of `@RequestParam` for this scenario? –  Aug 01 '21 at 20:32
  • `@RequestParam` means query parameter, so you need to pass it like `http://your-api?menuUuid=uuid-1,uuid-2,uuid-3`. To pass it in the request body, you need to make your endpoint POST, and accept your DTO using `@RequestBody menuUuidDto`. And DTO will be java class `class MenuUuuidDto { List menuItemUuid; } – geobreze Aug 01 '21 at 20:44
  • Many thanks, voted up. Actually it seems to work if I pass the array values ad form data by separating comma. However, is there a proper way to pass them as json as I generally use in Postman? (I use `@RequestParam` ). –  Aug 01 '21 at 20:53
  • Spring discards response body for GET requests, so you need to make your request POST, for example. https://stackoverflow.com/a/34956992/4655217. And then you need to accept request body: https://www.baeldung.com/spring-request-response-body#@requestbody. After that send request body via postman: https://learning.postman.com/docs/sending-requests/requests/#raw-data – geobreze Aug 01 '21 at 20:58
  • Thanks you very much for your helps. I marked it as answer and after try, I will ask additional question. Regards. –  Aug 02 '21 at 10:48