Search in sources :

Example 6 with HttpMessageWriter

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

the class WebFluxConfigurationSupportTests method responseEntityResultHandler.

@Test
public void responseEntityResultHandler() throws Exception {
    ApplicationContext context = loadConfig(WebFluxConfig.class);
    String name = "responseEntityResultHandler";
    ResponseEntityResultHandler handler = context.getBean(name, ResponseEntityResultHandler.class);
    assertNotNull(handler);
    assertEquals(0, handler.getOrder());
    List<HttpMessageWriter<?>> writers = handler.getMessageWriters();
    assertEquals(8, writers.size());
    assertHasMessageWriter(writers, byte[].class, APPLICATION_OCTET_STREAM);
    assertHasMessageWriter(writers, ByteBuffer.class, APPLICATION_OCTET_STREAM);
    assertHasMessageWriter(writers, String.class, TEXT_PLAIN);
    assertHasMessageWriter(writers, Resource.class, IMAGE_PNG);
    assertHasMessageWriter(writers, TestBean.class, APPLICATION_XML);
    assertHasMessageWriter(writers, TestBean.class, APPLICATION_JSON);
    assertHasMessageWriter(writers, TestBean.class, MediaType.parseMediaType("text/event-stream"));
    name = "webFluxContentTypeResolver";
    RequestedContentTypeResolver resolver = context.getBean(name, RequestedContentTypeResolver.class);
    assertSame(resolver, handler.getContentTypeResolver());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResponseEntityResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Test(org.junit.Test)

Example 7 with HttpMessageWriter

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

the class WebFluxConfigurationSupportTests method assertHasMessageWriter.

private void assertHasMessageWriter(List<HttpMessageWriter<?>> writers, Class<?> clazz, MediaType mediaType) {
    ResolvableType type = ResolvableType.forClass(clazz);
    assertTrue(writers.stream().anyMatch(c -> mediaType == null || c.canWrite(type, mediaType)));
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Validator(org.springframework.validation.Validator) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebHandler(org.springframework.web.server.WebHandler) ByteBuffer(java.nio.ByteBuffer) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) MimeType(org.springframework.util.MimeType) FreeMarkerConfigurer(org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer) APPLICATION_OCTET_STREAM(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM) HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) View(org.springframework.web.reactive.result.view.View) ResolvableType(org.springframework.core.ResolvableType) AbstractHandlerMapping(org.springframework.web.reactive.handler.AbstractHandlerMapping) Resource(org.springframework.core.io.Resource) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Ordered(org.springframework.core.Ordered) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConversionService(org.springframework.core.convert.ConversionService) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) ViewResolutionResultHandler(org.springframework.web.reactive.result.view.ViewResolutionResultHandler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) TEXT_PLAIN(org.springframework.http.MediaType.TEXT_PLAIN) Test(org.junit.Test) MimeTypeUtils(org.springframework.util.MimeTypeUtils) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ApplicationContext(org.springframework.context.ApplicationContext) ResponseEntityResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) IMAGE_PNG(org.springframework.http.MediaType.IMAGE_PNG) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) Bean(org.springframework.context.annotation.Bean) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert.assertEquals(org.junit.Assert.assertEquals) ResolvableType(org.springframework.core.ResolvableType)

Example 8 with HttpMessageWriter

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

the class BodyInsertersTests method createContext.

@Before
public void createContext() {
    final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriters.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    messageWriters.add(new ResourceHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
    messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
    messageWriters.add(new ServerSentEventHttpMessageWriter(Collections.singletonList(jsonEncoder)));
    messageWriters.add(new FormHttpMessageWriter());
    this.context = new BodyInserter.Context() {

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

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    };
    this.hints = new HashMap<>();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) Supplier(java.util.function.Supplier) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Before(org.junit.Before)

Example 9 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<>(new CharSequenceEncoder()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    } 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) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Aggregations

EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)9 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)9 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)6 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)6 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)5 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)5 Test (org.junit.Test)4 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)4 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)4 ArrayList (java.util.ArrayList)3 ApplicationContext (org.springframework.context.ApplicationContext)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 Before (org.junit.Before)2 ResolvableType (org.springframework.core.ResolvableType)2 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)2 ResponseEntityResultHandler (org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler)2 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1