Search in sources :

Example 1 with StringDecoder

use of org.springframework.core.codec.StringDecoder in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method addDefaultHttpMessageReaders.

/**
	 * Adds default converters that sub-classes can call from
	 * {@link #configureMessageReaders(List)} for {@code byte[]},
	 * {@code ByteBuffer}, {@code String}, {@code Resource}, JAXB2, and Jackson
	 * (if present on the classpath).
	 */
protected final void addDefaultHttpMessageReaders(List<HttpMessageReader<?>> readers) {
    readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder()));
    readers.add(new DecoderHttpMessageReader<>(new StringDecoder()));
    readers.add(new DecoderHttpMessageReader<>(new ResourceDecoder()));
    if (jaxb2Present) {
        readers.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
    }
    if (jackson2Present) {
        readers.add(new Jackson2ServerHttpMessageReader(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
    }
}
Also used : DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) ResourceDecoder(org.springframework.core.codec.ResourceDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) Jackson2ServerHttpMessageReader(org.springframework.http.codec.Jackson2ServerHttpMessageReader)

Example 2 with StringDecoder

use of org.springframework.core.codec.StringDecoder in project spring-framework by spring-projects.

the class DefaultExchangeStrategiesBuilder method defaultReaders.

private void defaultReaders() {
    messageReader(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    messageReader(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    messageReader(new ServerSentEventHttpMessageReader(sseDecoders()));
    messageReader(new DecoderHttpMessageReader<>(new StringDecoder(false)));
    if (jaxb2Present) {
        messageReader(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
    }
    if (jackson2Present) {
        messageReader(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
    }
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder)

Example 3 with StringDecoder

use of org.springframework.core.codec.StringDecoder in project spring-framework by spring-projects.

the class DefaultServerRequestTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockRequest.getHeaders()).thenReturn(httpHeaders);
    when(mockRequest.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultRequest.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 4 with StringDecoder

use of org.springframework.core.codec.StringDecoder in project spring-framework by spring-projects.

the class DefaultClientResponseTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockResponse.getHeaders()).thenReturn(httpHeaders);
    when(mockResponse.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultClientResponse.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 5 with StringDecoder

use of org.springframework.core.codec.StringDecoder in project spring-framework by spring-projects.

the class DefaultClientResponseTests method bodyToMonoError.

@Test
public void bodyToMonoError() throws Exception {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockResponse.getHeaders()).thenReturn(httpHeaders);
    when(mockResponse.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
    StepVerifier.create(resultMono).expectError(WebClientException.class).verify();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) StringDecoder(org.springframework.core.codec.StringDecoder) Test(org.junit.Test)

Aggregations

StringDecoder (org.springframework.core.codec.StringDecoder)13 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)11 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)10 Test (org.junit.Test)8 HttpHeaders (org.springframework.http.HttpHeaders)8 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)6 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)6 ByteBufferDecoder (org.springframework.core.codec.ByteBufferDecoder)4 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)4 Jaxb2XmlDecoder (org.springframework.http.codec.xml.Jaxb2XmlDecoder)4 ByteArrayDecoder (org.springframework.core.codec.ByteArrayDecoder)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FormHttpMessageReader (org.springframework.http.codec.FormHttpMessageReader)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Supplier (java.util.function.Supplier)1 Before (org.junit.Before)1 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)1