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());
}
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)));
}
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<>();
}
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);
}
Aggregations