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

The following examples show how to use org.springframework.http.codec.json.Jackson2JsonEncoder. 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
Source File: CodecConfigurerTests.java    From java-technology-stack with 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 #2
Source File: MessageWriterResultHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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
Source File: ResponseEntityResultHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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 #4
Source File: ServerSentEventHttpMessageWriterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #5
Source File: ServerCodecConfigurerTests.java    From java-technology-stack with 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 #6
Source File: CodecConfigurerTests.java    From spring-analysis-note with 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
Source File: ClientCodecConfigurerTests.java    From java-technology-stack with 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 #8
Source File: ClientCodecConfigurerTests.java    From spring-analysis-note with 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 #9
Source File: ServerCodecConfigurerTests.java    From spring-analysis-note with 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 #10
Source File: ServerSentEventHttpMessageWriterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #11
Source File: MessageWriterResultHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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 #12
Source File: ResponseEntityResultHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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 #13
Source File: JacksonJsonCodecInitializer.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Override
protected void register(GenericApplicationContext context, CodecConfigurer configurer) {
	ObjectMapper mapper = context.getBean(ObjectMapper.class);
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(mapper);
	configurer.customCodecs().decoder(new Jackson2JsonDecoder(mapper));
	configurer.customCodecs().encoder(encoder);
	configurer.customCodecs().writer(new ServerSentEventHttpMessageWriter(encoder));
}
 
Example #14
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
* Build the RestTemplate used to make HTTP requests.
* @return RestTemplate
*/
public static WebClient buildWebClient(ObjectMapper mapper) {
    ExchangeStrategies strategies = ExchangeStrategies
        .builder()
        .codecs(clientDefaultCodecsConfigurer -> {
            clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
            clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON));
        }).build();
    WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies);
    return webClient.build();
}
 
Example #15
Source File: ViewResolverRegistryTests.java    From java-technology-stack with 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 #16
Source File: CodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomWriters() {
	Encoder<?> customEncoder1 = mock(Encoder.class);
	Encoder<?> customEncoder2 = mock(Encoder.class);

	when(customEncoder1.canEncode(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customEncoder2.canEncode(ResolvableType.forClass(Object.class), null)).thenReturn(true);

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

	when(customWriter1.canWrite(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customWriter2.canWrite(ResolvableType.forClass(Object.class), null)).thenReturn(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());
	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(customEncoder1, getNextEncoder(writers));
	assertSame(customWriter1, writers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jackson2SmileEncoder.class, getNextEncoder(writers).getClass());
	assertEquals(Jaxb2XmlEncoder.class, getNextEncoder(writers).getClass());
	assertSame(customEncoder2, getNextEncoder(writers));
	assertSame(customWriter2, writers.get(this.index.getAndIncrement()));
	assertEquals(CharSequenceEncoder.class, getNextEncoder(writers).getClass());
}
 
Example #17
Source File: DemoClient.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
public static final void main(String[] args) throws IOException {
        WebClient client = WebClient.builder()
                //see: https://github.com/jetty-project/jetty-reactive-httpclient
                //.clientConnector(new JettyClientHttpConnector())
                .clientConnector(new ReactorClientHttpConnector())
                .codecs(
                        clientCodecConfigurer ->{
                            // use defaultCodecs() to apply DefaultCodecs
                            // clientCodecConfigurer.defaultCodecs();

                            // alter a registered encoder/decoder based on the default config.
                            // clientCodecConfigurer.defaultCodecs().jackson2Encoder(...)

                            // Or
                            // use customCodecs to register Codecs from scratch.
                            clientCodecConfigurer.customCodecs().register(new Jackson2JsonDecoder());
                            clientCodecConfigurer.customCodecs().register(new Jackson2JsonEncoder());
                        }

                )
                .exchangeStrategies(ExchangeStrategies.withDefaults())
//                .exchangeFunction(ExchangeFunctions.create(new ReactorClientHttpConnector())
//                        .filter(ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {})))
//                .filter(ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {clientRequest.}))
                //.defaultHeaders(httpHeaders -> httpHeaders.addAll())
                .baseUrl("http://localhost:8080")
                .build();
        client
            .get()
            .uri("/posts")
            .exchange()
            .flatMapMany(res -> res.bodyToFlux(Post.class))
            .log()
            .subscribe(post -> System.out.println("post: " + post));

        System.out.println("Client is started!");
        System.in.read();
    }
 
Example #18
Source File: CodecConfigurerTests.java    From java-technology-stack with 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 #19
Source File: CredHubWebClientFactory.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private static WebClient.Builder buildWebClient(String baseUri, ClientHttpConnector clientHttpConnector) {
	ExchangeStrategies strategies = ExchangeStrategies.builder().codecs((configurer) -> {
		ObjectMapper mapper = JsonUtils.buildObjectMapper();

		CodecConfigurer.DefaultCodecs dc = configurer.defaultCodecs();
		dc.jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
		dc.jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
	}).build();

	return WebClient.builder().clientConnector(clientHttpConnector).baseUrl(baseUri)
			.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
			.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
			.exchangeStrategies(strategies);
}
 
Example #20
Source File: ServerCodecConfigurerTests.java    From java-technology-stack with 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 File: ServerCodecConfigurerTests.java    From java-technology-stack with 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 #22
Source File: WebClientTransportClientFactory.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
private void setExchangeStrategies(WebClient.Builder builder) {
	ObjectMapper objectMapper = objectMapper();
	ExchangeStrategies strategies = ExchangeStrategies.builder()
			.codecs(clientDefaultCodecsConfigurer -> {
				clientDefaultCodecsConfigurer.defaultCodecs()
						.jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper,
								MediaType.APPLICATION_JSON));
				clientDefaultCodecsConfigurer.defaultCodecs()
						.jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper,
								MediaType.APPLICATION_JSON));

			}).build();
	builder.exchangeStrategies(strategies);
}
 
Example #23
Source File: RequestMappingMessageConversionIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #24
Source File: AdminApplicationDiscoveryTest.java    From spring-boot-admin with 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 File: AbstractAdminApplicationTest.java    From spring-boot-admin with 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 #26
Source File: BodyInsertersTests.java    From spring-analysis-note with 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 #27
Source File: ViewResolverRegistryTests.java    From spring-analysis-note with 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 #28
Source File: ResponseBodyResultHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #29
Source File: RequestMappingMessageConversionIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #30
Source File: CancelWithoutDemandCodecTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}