Created
November 16, 2022 22:05
-
-
Save mackatozis/4e18fed4f1aa596c03d36a032dd3d5cb to your computer and use it in GitHub Desktop.
Answer for https://stackoverflow.com/q/74346049/1930099
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import lombok.Data; | |
import org.springframework.http.MediaType; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import reactor.core.publisher.Flux; | |
@RestController | |
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) | |
public class MyController { | |
@GetMapping("/test") | |
Flux<UserDto> dtoFlux() { | |
return Flux.just(new UserDto(), new UserDto(), new UserDto()) | |
.flatMap(userDto -> MyService.findAll() | |
.collectList() | |
.map(relatedItemsAsField -> { | |
userDto.setRelatedItemsAsField(relatedItemsAsField); | |
return userDto; | |
})); | |
} | |
} | |
class MyService { | |
static Flux<Integer> findAll() { | |
// simulates Flux of User related data (e.g. Orders or Articles) | |
return Flux.just(1, 2, 3); | |
} | |
} | |
@Data | |
class UserDto { | |
private List<Integer> relatedItemsAsField; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment