use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ViewResolverRegistryTests method defaultViews.
@Test
public void defaultViews() throws Exception {
View view = new HttpMessageWriterView(new Jackson2JsonEncoder());
this.registry.defaultViews(view);
assertThat(this.registry.getDefaultViews().size()).isEqualTo(1);
assertThat(this.registry.getDefaultViews().get(0)).isSameAs(view);
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class CancelWithoutDemandCodecTests method cancelWithJackson.
// gh-22107
@Test
public void cancelWithJackson() {
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
Flux<DataBuffer> flux = encoder.encode(Flux.just(new Pojo("foofoo", "barbar"), new Pojo("bar", "baz")), this.bufferFactory, ResolvableType.forClass(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());
BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
// Assume sync execution (e.g. encoding with Flux.just)..
flux.subscribe(subscriber);
subscriber.cancel();
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder 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<>(CharSequenceEncoder.textPlainOnly()));
writerList.add(new ResourceHttpMessageWriter());
writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
} else {
writerList = Arrays.asList(writers);
}
RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
return new ResponseEntityResultHandler(writerList, resolver);
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriterTests method writePojoWithPrettyPrint.
// SPR-14899
@ParameterizedDataBufferAllocatingTest
void writePojoWithPrettyPrint(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory;
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));
MockServerHttpResponse outputMessage = new MockServerHttpResponse(super.bufferFactory);
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
testWrite(source, outputMessage, Pojo.class);
StepVerifier.create(outputMessage.getBody()).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data: \"foo\" : \"foofoo\",\n" + "data: \"bar\" : \"barbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).consumeNextWith(stringConsumer("data:")).consumeNextWith(stringConsumer("{\n" + "data: \"foo\" : \"foofoofoo\",\n" + "data: \"bar\" : \"barbarbar\"\n" + "data:}")).consumeNextWith(stringConsumer("\n\n")).expectComplete().verify();
}
use of org.springframework.http.codec.json.Jackson2JsonEncoder in project spring-framework by spring-projects.
the class CodecConfigurerTests method encoderDecoderOverrides.
@Test
void encoderDecoderOverrides() {
Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
Jackson2SmileDecoder smileDecoder = new Jackson2SmileDecoder();
Jackson2SmileEncoder smileEncoder = new Jackson2SmileEncoder();
ProtobufDecoder protobufDecoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
ProtobufEncoder protobufEncoder = new ProtobufEncoder();
Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
this.configurer.defaultCodecs().jackson2SmileDecoder(smileDecoder);
this.configurer.defaultCodecs().jackson2SmileEncoder(smileEncoder);
this.configurer.defaultCodecs().protobufDecoder(protobufDecoder);
this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);
assertDecoderInstance(jacksonDecoder);
assertDecoderInstance(smileDecoder);
assertDecoderInstance(protobufDecoder);
assertDecoderInstance(jaxb2Decoder);
assertEncoderInstance(jacksonEncoder);
assertEncoderInstance(smileEncoder);
assertEncoderInstance(protobufEncoder);
assertEncoderInstance(jaxb2Encoder);
}
Aggregations