Search in sources :

Example 6 with Jackson2JsonEncoder

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);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) ResourceHttpMessageReader(org.springframework.http.codec.ResourceHttpMessageReader) 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) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 7 with Jackson2JsonEncoder

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));
}
Also used : Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 8 with Jackson2JsonEncoder

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);
}
Also used : MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 9 with Jackson2JsonEncoder

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);
}
Also used : DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) ResourceHttpMessageReader(org.springframework.http.codec.ResourceHttpMessageReader) MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) PartHttpMessageWriter(org.springframework.http.codec.multipart.PartHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 10 with Jackson2JsonEncoder

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 + "}");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MediaType(org.springframework.http.MediaType) MethodParameter(org.springframework.core.MethodParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)24 Test (org.junit.jupiter.api.Test)14 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)13 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)13 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)11 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)9 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)9 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)8 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)7 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)7 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)6 ArrayList (java.util.ArrayList)5 ServerSentEventHttpMessageReader (org.springframework.http.codec.ServerSentEventHttpMessageReader)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ProtobufHttpMessageWriter (org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)4 HashMap (java.util.HashMap)3 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)3 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)3 CodecConfigurer (org.springframework.http.codec.CodecConfigurer)3 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)3