Когда каждый элемент String завершается с символом новой строки Flux «возвращается, как ожидалось» через ServerResponse и подписку на возвращаемый Flux дает ожидаемые результаты. Однако, если рассматривать его как простой JSON (через Postman), это также приводит к тому, что в теле JSON возвращается дополнительный пустой элемент String.
Вывод консоли демонстрирует описанное поведение ...
- Первый список элементов String происходит в StringProducerHandler.getAll() и указывает результаты Flux, содержащий 10 элементов String, где второе вхождение каждого значения String заканчивается символом новой строки, в результате чего выводится пустая строка.
- Второй список элементов String находится в StringClient.getAll() и демонстрирует, как исходный Строковые элементы, которые не завершались символом новой строки, были объединены со следующим элементом.
2019-10-10 10:13:37.225 INFO 8748 - -- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty запущен на портах: 8080
2019-10-10 10:13:37.228 INFO 8748 --- [ main] c.example.fluxtest .FluxTestApplication: FluxTestApplication запущен за 1,271 секунды (JVM работает для версии 1,796)
***** «Get» выдается на http:/localhost:8080/StringClient/StringStringClientHandler.getAll( ServerRequest )
StringClient.getAll()
StringProducerHandler.getAll( ServerRequest )
ListElement-0
ListElement-0
ListElement-1
ListElement-1
ListElement-2
ListElement-2
ListElement-3
ListElement-3
ListElement-4
ListElement-4
ListElement-0ListElement-0 @ 1570727628948
ListElement-1ListElement-1 @ 1570727628948
ListElement-2ListElement-2 @ 1570727628948
ListElement-3ListElement-3 @ 1570727628 948
ListElement-4ListElement-4 @ 1570727628949
@SpringBootApplication
public class FluxTestApplication {
public static void main(String[] args) {
SpringApplication.run(FluxTestApplication.class, args);
}
}
@Configuration
public class StringClientRouter {
@Bean
public RouterFunction clientRoutes(StringClientHandler requestHandler) {
return nest(path("/StringClient"),
nest(accept(APPLICATION_JSON),
RouterFunctions.route(RequestPredicates.GET("/String"), requestHandler::getAll)));
}
}
@Component
public class StringClientHandler {
@Autowired
StringClient stringClient;
public Mono getAll(ServerRequest request) {
System.out.println("StringClientHandler.getAll( ServerRequest )");
Mono signal = stringClient.getAll();
return ServerResponse.ok().build();
}
}
@Component
public class StringClient {
private final WebClient client;
public StringClient() {
client = WebClient.create();
}
public Mono getAll() {
System.out.println("StringClient.getAll()");
// break chain to explicitly obtain the ClientResponse
Mono monoCR = client.get().uri("http://localhost:8080/StringProducer/String")
.accept(MediaType.APPLICATION_JSON)
.exchange();
// extract the Flux and print to console
Flux fluxString = monoCR.flatMapMany(response -> response.bodyToFlux(String.class));
// this statement iterates over the Flux and outputs each element
fluxString.subscribe(strVal -> System.out.println(strVal + " @ " + System.currentTimeMillis()));
return Mono.empty();
}
}
@Configuration
public class StringProducerRouter {
@Bean
public RouterFunction demoPOJORoute(StringProducerHandler requestHandler) {
return nest(path("/StringProducer"),
nest(accept(APPLICATION_JSON),
RouterFunctions.route(RequestPredicates.GET("/String"), requestHandler::getAll)));
}
}
@Component
public class StringProducerHandler {
public Mono getAll(ServerRequest request) {
System.out.println("StringProducerHandler.getAll( ServerRequest )");
int listSize = 5;
List strList = new ArrayList();
for (int i=0; i
Подробнее здесь: https://stackoverflow.com/questions/583 ... ed-via-ser