Java Code Examples for org.springframework.http.codec.json.Jackson2JsonEncoder

The following examples show how to use org.springframework.http.codec.json.Jackson2JsonEncoder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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);
}
 
Example 2
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) {};
}
 
Example 3
@Test  // SPR-14899
public void writePojoWithPrettyPrint() {
	ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
	this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));

	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:{\n" +
					"data:  \"foo\" : \"foofoo\",\n" +
					"data:  \"bar\" : \"barbar\"\n" + "data:}\n\n"))
			.consumeNextWith(stringConsumer("data:{\n" +
					"data:  \"foo\" : \"foofoofoo\",\n" +
					"data:  \"bar\" : \"barbarbar\"\n" + "data:}\n\n"))
			.expectComplete()
			.verify();
}
 
Example 4
Source Project: spring-analysis-note   Source File: ServerCodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void defaultWriters() {
	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
	assertEquals(11, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertStringEncoder(getNextEncoder(writers), true);
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertSseWriter(writers);
	assertStringEncoder(getNextEncoder(writers), false);
}
 
Example 5
Source Project: spring-analysis-note   Source File: ClientCodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void defaultWriters() {
	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
	assertEquals(11, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertStringEncoder(getNextEncoder(writers), true);
	assertEquals(MultipartHttpMessageWriter.class, writers.get(this.index.getAndIncrement()).getClass());
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertStringEncoder(getNextEncoder(writers), false);
}
 
Example 6
Source Project: spring-analysis-note   Source File: CodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void encoderDecoderOverrides() {
	Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
	Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
	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().protobufDecoder(protobufDecoder);
	this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
	this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
	this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);

	assertDecoderInstance(jacksonDecoder);
	assertDecoderInstance(protobufDecoder);
	assertDecoderInstance(jaxb2Decoder);
	assertEncoderInstance(jacksonEncoder);
	assertEncoderInstance(protobufEncoder);
	assertEncoderInstance(jaxb2Encoder);
}
 
Example 7
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);
}
 
Example 8
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) {};
}
 
Example 9
@Test  // SPR-14899
public void writePojoWithPrettyPrint() {
	ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
	this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper));

	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"))
			.consumeNextWith(stringConsumer("\n"))
			.consumeNextWith(stringConsumer("data:"))
			.consumeNextWith(stringConsumer("{\n" +
					"data:  \"foo\" : \"foofoofoo\",\n" +
					"data:  \"bar\" : \"barbarbar\"\n" + "data:}"))
			.consumeNextWith(stringConsumer("\n"))
			.consumeNextWith(stringConsumer("\n"))
			.expectComplete()
			.verify();
}
 
Example 10
Source Project: java-technology-stack   Source File: ServerCodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void defaultWriters() {
	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
	assertEquals(11, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertStringEncoder(getNextEncoder(writers), true);
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertSseWriter(writers);
	assertStringEncoder(getNextEncoder(writers), false);
}
 
Example 11
Source Project: java-technology-stack   Source File: ClientCodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void defaultWriters() {
	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
	assertEquals(11, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertStringEncoder(getNextEncoder(writers), true);
	assertEquals(MultipartHttpMessageWriter.class, writers.get(this.index.getAndIncrement()).getClass());
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertStringEncoder(getNextEncoder(writers), false);
}
 
Example 12
Source Project: java-technology-stack   Source File: CodecConfigurerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void encoderDecoderOverrides() {
	Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
	Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
	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().protobufDecoder(protobufDecoder);
	this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
	this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
	this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);

	assertDecoderInstance(jacksonDecoder);
	assertDecoderInstance(protobufDecoder);
	assertDecoderInstance(jaxb2Decoder);
	assertEncoderInstance(jacksonEncoder);
	assertEncoderInstance(protobufEncoder);
	assertEncoderInstance(jaxb2Encoder);
}
 
Example 13
Source Project: spring-analysis-note   Source File: BodyInsertersTests.java    License: MIT License 5 votes vote down vote up
@Before
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<>();
}
 
Example 14
Source Project: spring-analysis-note   Source File: ViewResolverRegistryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultViews() throws Exception {
	View view = new HttpMessageWriterView(new Jackson2JsonEncoder());
	this.registry.defaultViews(view);

	assertEquals(1, this.registry.getDefaultViews().size());
	assertSame(view, this.registry.getDefaultViews().get(0));
}
 
Example 15
@Before
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);
}
 
Example 16
@GetMapping("/publisher")
public Publisher<ByteBuffer> getPublisher() {
	DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory,
			ResolvableType.forClass(Person.class), JSON, Collections.emptyMap()).map(DataBuffer::asByteBuffer);
}
 
Example 17
@Test // gh-22107
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();
	flux.subscribe(subscriber); // Assume sync execution (e.g. encoding with Flux.just)..
	subscriber.cancel();
}
 
Example 18
@Test // gh-22107
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));
}
 
Example 19
Source Project: spring-analysis-note   Source File: ServerCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

	assertSame(encoder, this.configurer.getWriters().stream()
			.filter(writer -> ServerSentEventHttpMessageWriter.class.equals(writer.getClass()))
			.map(writer -> (ServerSentEventHttpMessageWriter) writer)
			.findFirst()
			.map(ServerSentEventHttpMessageWriter::getEncoder)
			.filter(e -> e == encoder).orElse(null));
}
 
Example 20
Source Project: spring-analysis-note   Source File: ServerCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
private void assertSseWriter(List<HttpMessageWriter<?>> writers) {
	HttpMessageWriter<?> writer = writers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageWriter.class, writer.getClass());
	Encoder<?> encoder = ((ServerSentEventHttpMessageWriter) writer).getEncoder();
	assertNotNull(encoder);
	assertEquals(Jackson2JsonEncoder.class, encoder.getClass());
}
 
Example 21
Source Project: spring-analysis-note   Source File: CodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultWriters() {
	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
	assertEquals(10, writers.size());
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertStringEncoder(getNextEncoder(writers), true);
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertStringEncoder(getNextEncoder(writers), false);
}
 
Example 22
Source Project: spring-analysis-note   Source File: CodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomWriters() {
	Encoder<?> customEncoder1 = mock(Encoder.class);
	Encoder<?> customEncoder2 = mock(Encoder.class);

	given(customEncoder1.canEncode(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customEncoder2.canEncode(ResolvableType.forClass(Object.class), null)).willReturn(true);

	HttpMessageWriter<?> customWriter1 = mock(HttpMessageWriter.class);
	HttpMessageWriter<?> customWriter2 = mock(HttpMessageWriter.class);

	given(customWriter1.canWrite(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customWriter2.canWrite(ResolvableType.forClass(Object.class), null)).willReturn(true);

	this.configurer.customCodecs().encoder(customEncoder1);
	this.configurer.customCodecs().encoder(customEncoder2);

	this.configurer.customCodecs().writer(customWriter1);
	this.configurer.customCodecs().writer(customWriter2);

	List<HttpMessageWriter<?>> writers = this.configurer.getWriters();

	assertEquals(14, writers.size());
	assertSame(customEncoder1, getNextEncoder(writers));
	assertSame(customWriter1, writers.get(this.index.getAndIncrement()));
	assertEquals(ByteArrayEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ByteBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(DataBufferEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ResourceHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(ProtobufHttpMessageWriter.class, writers.get(index.getAndIncrement()).getClass());
	assertSame(customEncoder2, getNextEncoder(writers));
	assertSame(customWriter2, writers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
}
 
Example 23
Source Project: Moss   Source File: AbstractAdminApplicationTest.java    License: Apache License 2.0 5 votes vote down vote up
protected WebTestClient createWebClient(int port) {
    ObjectMapper mapper = new ObjectMapper().registerModule(new JsonOrgModule());
    return WebTestClient.bindToServer()
                        .baseUrl("http://localhost:" + port)
                        .exchangeStrategies(ExchangeStrategies.builder().codecs((configurer) -> {
                            configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
                            configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
                        }).build())
                        .build();
}
 
Example 24
Source Project: Moss   Source File: AdminApplicationDiscoveryTest.java    License: Apache License 2.0 5 votes vote down vote up
private WebTestClient createWebClient(int port) {
    ObjectMapper mapper = new ObjectMapper().registerModule(new JsonOrgModule());
    return WebTestClient.bindToServer()
                        .baseUrl("http://localhost:" + port)
                        .exchangeStrategies(ExchangeStrategies.builder().codecs((configurer) -> {
                            configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
                            configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
                        }).build())
                        .build();
}
 
Example 25
Source Project: java-technology-stack   Source File: BodyInsertersTests.java    License: MIT License 5 votes vote down vote up
@Before
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<>();
}
 
Example 26
Source Project: java-technology-stack   Source File: ViewResolverRegistryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void defaultViews() throws Exception {
	View view = new HttpMessageWriterView(new Jackson2JsonEncoder());
	this.registry.defaultViews(view);

	assertEquals(1, this.registry.getDefaultViews().size());
	assertSame(view, this.registry.getDefaultViews().get(0));
}
 
Example 27
@Before
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);
}
 
Example 28
@GetMapping("/publisher")
public Publisher<ByteBuffer> getPublisher() {
	DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory,
			ResolvableType.forClass(Person.class), JSON, Collections.emptyMap()).map(DataBuffer::asByteBuffer);
}
 
Example 29
Source Project: java-technology-stack   Source File: ServerCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

	assertSame(encoder, this.configurer.getWriters().stream()
			.filter(writer -> ServerSentEventHttpMessageWriter.class.equals(writer.getClass()))
			.map(writer -> (ServerSentEventHttpMessageWriter) writer)
			.findFirst()
			.map(ServerSentEventHttpMessageWriter::getEncoder)
			.filter(e -> e == encoder).orElse(null));
}
 
Example 30
Source Project: java-technology-stack   Source File: ServerCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
private void assertSseWriter(List<HttpMessageWriter<?>> writers) {
	HttpMessageWriter<?> writer = writers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageWriter.class, writer.getClass());
	Encoder<?> encoder = ((ServerSentEventHttpMessageWriter) writer).getEncoder();
	assertNotNull(encoder);
	assertEquals(Jackson2JsonEncoder.class, encoder.getClass());
}