use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ClientCodecConfigurerTests method jackson2CodecCustomization.
@Test
public void jackson2CodecCustomization() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);
assertThat(findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder()).isSameAs(decoder);
List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class CancelWithoutDemandCodecTests method cancelWithSse.
// gh-22107
@Test
public void cancelWithSse() {
ServerSentEvent<?> event = ServerSentEvent.builder().data("bar").id("c42").event("foo").build();
ServerSentEventHttpMessageWriter writer = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder());
CancellingOutputMessage outputMessage = new CancellingOutputMessage(this.bufferFactory);
writer.write(Mono.just(event), ResolvableType.forClass(ServerSentEvent.class), MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).block(Duration.ofSeconds(5));
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ServerCodecConfigurerTests method cloneConfigurer.
@Test
public void cloneConfigurer() {
ServerCodecConfigurer clone = this.configurer.clone();
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(new DefaultPartHttpMessageReader());
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
clone.defaultCodecs().multipartReader(reader);
clone.defaultCodecs().serverSentEventEncoder(encoder);
// Clone has the customizations
HttpMessageReader<?> actualReader = findCodec(clone.getReaders(), MultipartHttpMessageReader.class);
ServerSentEventHttpMessageWriter actualWriter = findCodec(clone.getWriters(), ServerSentEventHttpMessageWriter.class);
assertThat(actualReader).isSameAs(reader);
assertThat(actualWriter.getEncoder()).isSameAs(encoder);
// Original does not have the customizations
actualReader = findCodec(this.configurer.getReaders(), MultipartHttpMessageReader.class);
actualWriter = findCodec(this.configurer.getWriters(), ServerSentEventHttpMessageWriter.class);
assertThat(actualReader).isNotSameAs(reader);
assertThat(actualWriter.getEncoder()).isNotSameAs(encoder);
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ServerCodecConfigurerTests method jackson2EncoderOverride.
@Test
public void jackson2EncoderOverride() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);
List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
assertThat(findCodec(writers, ServerSentEventHttpMessageWriter.class).getEncoder()).isSameAs(encoder);
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ResponseEntityResultHandlerTests method handleWithObjectMapperByTypeRegistration.
// gh-26212
@Test
public void handleWithObjectMapperByTypeRegistration() {
MediaType halFormsMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
MediaType halMediaType = MediaType.parseMediaType("application/hal+json");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
encoder.registerObjectMappersForType(Person.class, map -> map.put(halMediaType, objectMapper));
EncoderHttpMessageWriter<?> writer = new EncoderHttpMessageWriter<>(encoder);
ResponseEntityResultHandler handler = new ResponseEntityResultHandler(Collections.singletonList(writer), new RequestedContentTypeResolverBuilder().build());
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").header("Accept", halFormsMediaType + "," + halMediaType));
ResponseEntity<Person> value = ResponseEntity.ok().body(new Person("Jason"));
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Person.class));
HandlerResult result = handlerResult(value, returnType);
handler.handleResult(exchange, result).block();
assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(halMediaType);
assertThat(exchange.getResponse().getBodyAsString().block()).isEqualTo("{" + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}");
}
Aggregations