Search in sources :

Example 6 with StringDecoder

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

the class DefaultHandlerStrategiesBuilder method defaultConfiguration.

public void defaultConfiguration() {
    messageReader(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    messageReader(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    messageReader(new DecoderHttpMessageReader<>(new StringDecoder()));
    messageReader(new FormHttpMessageReader());
    messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    messageWriter(new ResourceHttpMessageWriter());
    if (jaxb2Present) {
        messageReader(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
        messageWriter(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        messageReader(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
        Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
        messageWriter(new Jackson2ServerHttpMessageWriter(jsonEncoder));
        messageWriter(new ServerSentEventHttpMessageWriter(Collections.singletonList(jsonEncoder)));
    } else {
        messageWriter(new ServerSentEventHttpMessageWriter());
    }
    localeResolver(DEFAULT_LOCALE_RESOLVER);
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) StringDecoder(org.springframework.core.codec.StringDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) Jackson2ServerHttpMessageWriter(org.springframework.http.codec.Jackson2ServerHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder)

Example 7 with StringDecoder

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

the class HttpEntityArgumentResolverTests method createResolver.

private HttpEntityArgumentResolver createResolver() {
    List<HttpMessageReader<?>> readers = new ArrayList<>();
    readers.add(new DecoderHttpMessageReader<>(new StringDecoder()));
    return new HttpEntityArgumentResolver(readers, new ReactiveAdapterRegistry());
}
Also used : HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ArrayList(java.util.ArrayList) StringDecoder(org.springframework.core.codec.StringDecoder)

Example 8 with StringDecoder

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

the class BodyExtractorsTests method createContext.

@Before
public void createContext() {
    final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
    messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    messageReaders.add(new DecoderHttpMessageReader<>(new StringDecoder()));
    messageReaders.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
    messageReaders.add(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
    messageReaders.add(new FormHttpMessageReader());
    this.context = new BodyExtractor.Context() {

        @Override
        public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
            return messageReaders::stream;
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    };
    this.hints = new HashMap<String, Object>();
}
Also used : Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ArrayList(java.util.ArrayList) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) Supplier(java.util.function.Supplier) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) Before(org.junit.Before)

Example 9 with StringDecoder

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

the class DefaultClientResponseTests method bodyToFluxError.

@Test
public void bodyToFluxError() throws Exception {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockResponse.getHeaders()).thenReturn(httpHeaders);
    when(mockResponse.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
    StepVerifier.create(resultFlux).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)

Example 10 with StringDecoder

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

the class DefaultClientResponseTests method bodyToMono.

@Test
public void bodyToMono() 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.getStatusCode()).thenReturn(HttpStatus.OK);
    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.bodyToMono(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)

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