Skip to content

Instantly share code, notes, and snippets.

@mackatozis
Created November 16, 2022 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackatozis/4e18fed4f1aa596c03d36a032dd3d5cb to your computer and use it in GitHub Desktop.
Save mackatozis/4e18fed4f1aa596c03d36a032dd3d5cb to your computer and use it in GitHub Desktop.
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