Search in sources :

Example 11 with Jackson2JsonEncoder

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

the class MessageWriterResultHandlerTests method initResultHandler.

private AbstractMessageWriterResultHandler initResultHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
        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 AbstractMessageWriterResultHandler(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)

Example 12 with Jackson2JsonEncoder

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

the class ResponseBodyResultHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
    writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    writerList.add(new ResourceHttpMessageWriter());
    writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    this.resultHandler = new ResponseBodyResultHandler(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) ArrayList(java.util.ArrayList) 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) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with Jackson2JsonEncoder

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

the class ServerSentEventHttpMessageWriterTests method writePojoWithPrettyPrint.

// SPR-14899
@Test
public void writePojoWithPrettyPrint() {
    ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
    this.messageWriter = new ServerSentEventHttpMessageWriter(Collections.singletonList(new Jackson2JsonEncoder(mapper)));
    Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    MockServerHttpResponse outputMessage = new MockServerHttpResponse();
    messageWriter.write(source, ResolvableType.forClass(Pojo.class), MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).block(Duration.ofMillis(5000));
    StepVerifier.create(outputMessage.getBodyAsString()).expectNext("data:{\n" + "data:  \"foo\" : \"foofoo\",\n" + "data:  \"bar\" : \"barbar\"\n" + "data:}\n\n" + "data:{\n" + "data:  \"foo\" : \"foofoofoo\",\n" + "data:  \"bar\" : \"barbarbar\"\n" + "data:}\n\n").expectComplete().verify();
}
Also used : MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.Test)

Example 14 with Jackson2JsonEncoder

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

the class DefaultExchangeStrategiesBuilder method defaultWriters.

private void defaultWriters() {
    messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    messageWriter(new ResourceHttpMessageWriter());
    messageWriter(new FormHttpMessageWriter());
    if (jaxb2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    }
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder)

Example 15 with Jackson2JsonEncoder

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

the class BodyInsertersTests method createContext.

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

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return messageWriters;
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.empty();
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    };
    this.hints = new HashMap<>();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) 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) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Optional(java.util.Optional) 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) List(java.util.List) ArrayList(java.util.ArrayList) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

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