Search in sources :

Example 21 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class ClientCodecConfigurerTests method cloneShouldNotDropMultipartCodecs.

// gh-24194
@Test
public void cloneShouldNotDropMultipartCodecs() {
    ClientCodecConfigurer clone = this.configurer.clone();
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(writers).hasSize(12);
}
Also used : FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Example 22 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class ClientCodecConfigurerTests method clonedConfigurer.

@Test
public void clonedConfigurer() {
    ClientCodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jackson2Decoder = new Jackson2JsonDecoder();
    clone.defaultCodecs().serverSentEventDecoder(jackson2Decoder);
    clone.defaultCodecs().multipartCodecs().encoder(new Jackson2SmileEncoder());
    clone.defaultCodecs().multipartCodecs().writer(new ResourceHttpMessageWriter());
    // Clone has the customizations
    Decoder<?> sseDecoder = findCodec(clone.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isSameAs(jackson2Decoder);
    assertThat(writers).hasSize(2);
    // Original does not have the customizations
    sseDecoder = findCodec(this.configurer.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    writers = findCodec(this.configurer.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isNotSameAs(jackson2Decoder);
    assertThat(writers).hasSize(12);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Example 23 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class HttpMessageWriterView method write.

@SuppressWarnings("unchecked")
private <T> Mono<Void> write(T value, @Nullable MediaType contentType, ServerWebExchange exchange) {
    Publisher<T> input = Mono.justOrEmpty(value);
    ResolvableType elementType = ResolvableType.forClass(value.getClass());
    return ((HttpMessageWriter<T>) this.writer).write(input, elementType, contentType, exchange.getResponse(), Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix()));
}
Also used : EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResolvableType(org.springframework.core.ResolvableType)

Example 24 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method setup.

@BeforeEach
void setup() {
    List<HttpMessageReader<?>> readers = ServerCodecConfigurer.create().getReaders();
    ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
    this.resolver = new RequestPartMethodArgumentResolver(readers, registry);
    List<HttpMessageWriter<?>> writers = ClientCodecConfigurer.create().getWriters();
    this.writer = new MultipartHttpMessageWriter(writers);
}
Also used : HttpMessageReader(org.springframework.http.codec.HttpMessageReader) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 25 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method createHandler.

private ResponseEntityResultHandler createHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    } else {
        writerList = Arrays.asList(writers);
    }
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    return new ResponseEntityResultHandler(writerList, resolver);
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Aggregations

HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)27 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)23 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)15 Test (org.junit.jupiter.api.Test)14 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)11 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)11 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)11 ArrayList (java.util.ArrayList)10 ProtobufHttpMessageWriter (org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)8 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)7 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)6 List (java.util.List)5 MediaType (org.springframework.http.MediaType)5 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)5 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)5 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)5 ResolvableType (org.springframework.core.ResolvableType)4 BodyInserter (org.springframework.web.reactive.function.BodyInserter)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3