reactor.core.publisher.Flux.just()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(558)

本文整理了Java中reactor.core.publisher.Flux.just()方法的一些代码示例,展示了Flux.just()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Flux.just()方法的具体详情如下:
包路径:reactor.core.publisher.Flux
类名称:Flux
方法名:just

Flux.just介绍

[英]Create a new Flux that will only emit a single element then onComplete.
[中]创建一个新的通量,它只会发出一个元素,然后就完成了。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-14952
public void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception {
  TestServerHttpResponse response = new TestServerHttpResponse();
  Flux<Flux<DefaultDataBuffer>> flux = Flux.just(Flux.just(wrap("foo")));
  response.writeAndFlushWith(flux).block();
  assertTrue(response.statusCodeWritten);
  assertTrue(response.headersWritten);
  assertTrue(response.cookiesWritten);
  assertEquals(1, response.body.size());
  assertEquals("foo", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
    DataBuffer buffer = response.bufferFactory().allocateBuffer(body.length);
    buffer.write(body);
    return response.writeAndFlushWith(Flux.just(Flux.just(buffer)));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void bodyToFluxParameterizedTypeReference() {
  Flux<String> result = Flux.just("foo");
  ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>() {};
  when(mockRequest.bodyToFlux(reference)).thenReturn(result);
  assertSame(result, wrapper.bodyToFlux(reference));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void bodyToFluxParameterizedTypeReference() {
  Flux<String> result = Flux.just("foo");
  ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>() {};
  when(mockResponse.bodyToFlux(reference)).thenReturn(result);
  assertSame(result, wrapper.bodyToFlux(reference));
}

代码示例来源:origin: spring-projects/spring-framework

private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
  byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
  DataBuffer buffer = new DefaultDataBufferFactory().wrap(bytes);
  return exchange.getResponse().writeWith(Flux.just(buffer));
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-16402
public void singleSubscriberWithStrings() {
  UnicastProcessor<String> processor = UnicastProcessor.create();
  Flux.just("foo", "bar", "baz").subscribe(processor);
  MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
  bodyBuilder.asyncPart("name", processor, String.class);
  Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
  Map<String, Object> hints = Collections.emptyMap();
  this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void setContentLengthForMonoBody() {
  DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
  DataBuffer buffer = factory.wrap("body".getBytes(StandardCharsets.UTF_8));
  HttpMessageWriter<String> writer = getWriter(Flux.just(buffer), MimeTypeUtils.TEXT_PLAIN);
  writer.write(Mono.just("body"), forClass(String.class), TEXT_PLAIN, this.response, NO_HINTS).block();
  assertEquals(4, this.response.getHeaders().getContentLength());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void writeOneItem() throws Exception {
  Mono<Void> completion = Flux.just("one").as(this::sendOperator);
  Signal<Void> signal = completion.materialize().block();
  assertNotNull(signal);
  assertTrue("Unexpected signal: " + signal, signal.isOnComplete());
  assertEquals(1, this.writer.items.size());
  assertEquals("one", this.writer.items.get(0));
  assertTrue(this.writer.completed);
}

代码示例来源:origin: spring-projects/spring-framework

private ServerHttpRequest generateErrorMultipartRequest() {
  return MockServerHttpRequest.post("/")
      .header(CONTENT_TYPE, MULTIPART_FORM_DATA.toString())
      .body(Flux.just(new DefaultDataBufferFactory().wrap("invalid content".getBytes())));
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
  assertEquals(StringDecoder.class, decoder.getClass());
  assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
  assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM));
  Flux<String> flux = (Flux<String>) decoder.decode(
      Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
      ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());
  assertEquals(Arrays.asList("line1", "line2"), flux.collectList().block(Duration.ZERO));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  @Test
  public void encode() {
    Flux<ByteBuffer> input = Flux.just(this.fooBytes, this.barBytes)
        .map(ByteBuffer::wrap);

    testEncodeAll(input, ByteBuffer.class, step -> step
        .consumeNextWith(expectBytes(this.fooBytes))
        .consumeNextWith(expectBytes(this.barBytes))
        .verifyComplete());

  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void decodeEmptyDataBuffer() {
  Flux<DataBuffer> input = Flux.just(stringBuffer(""));
  Flux<String> output = this.decoder.decode(input,
      TYPE, null, Collections.emptyMap());
  StepVerifier.create(output)
      .expectNext("")
      .expectComplete().verify();
}

代码示例来源:origin: spring-projects/spring-framework

public Mono<ServerResponse> flux(ServerRequest request) {
    Person person1 = new Person("John");
    Person person2 = new Person("Jane");
    return ServerResponse.ok().body(
        fromPublisher(Flux.just(person1, person2), Person.class));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fromMultipartData() {
  MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
  map.set("name 3", "value 3");
  BodyInserters.FormInserter<Object> inserter =
      BodyInserters.fromMultipartData("name 1", "value 1")
          .withPublisher("name 2", Flux.just("foo", "bar", "baz"), String.class)
          .with(map);
  MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com"));
  Mono<Void> result = inserter.insert(request, this.context);
  StepVerifier.create(result).expectComplete().verify();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void errorInStream() {
  DataBuffer buffer = stringBuffer("{\"id\":1,\"name\":");
  Flux<DataBuffer> source = Flux.just(buffer)
      .concatWith(Flux.error(new RuntimeException()));
  Flux<TokenBuffer> result = Jackson2Tokenizer.tokenize(source, this.jsonFactory, true);
  StepVerifier.create(result)
      .expectError(RuntimeException.class)
      .verify();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void writeWritableByteChannel() throws Exception {
  DataBuffer foo = stringBuffer("foo");
  DataBuffer bar = stringBuffer("bar");
  DataBuffer baz = stringBuffer("baz");
  DataBuffer qux = stringBuffer("qux");
  Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux);
  WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
  Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
  verifyWrittenData(writeResult);
  channel.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-16521
public void jsonEOFExceptionIsWrappedAsDecodingError() {
  Flux<DataBuffer> source = Flux.just(stringBuffer("{\"status\": \"noClosingQuote}"));
  Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(source, this.jsonFactory, false);
  StepVerifier.create(tokens)
      .expectError(DecodingException.class)
      .verify();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readFormError() {
  DataBuffer fooBuffer = stringBuffer("name=value");
  Flux<DataBuffer> body =
      Flux.just(fooBuffer).concatWith(Flux.error(new RuntimeException()));
  MockServerHttpRequest request = request(body);
  Flux<MultiValueMap<String, String>> result = this.reader.read(null, request, null);
  StepVerifier.create(result)
      .expectError()
      .verify();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void takeUntilByteCount() {
  Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(
      Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L);
  StepVerifier.create(result)
      .consumeNextWith(stringConsumer("foo"))
      .consumeNextWith(stringConsumer("ba"))
      .expectComplete()
      .verify(Duration.ofSeconds(5));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void encode() throws Exception {
  Flux<Object> input = Flux.just(new Pojo("foo", "bar"),
      new Pojo("foofoo", "barbar"),
      new Pojo("foofoofoo", "barbarbar"));
  testEncodeAll(input, ResolvableType.forClass(Pojo.class), step -> step
      .consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
      .consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
      .consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
      .verifyComplete(),
      APPLICATION_STREAM_JSON, null);
}

相关文章

微信公众号

最新文章

更多

Flux类方法