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

The following examples show how to use org.springframework.http.codec.json.Jackson2JsonDecoder. 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 defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(11, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #2
Source File: ServerCodecConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(13, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(DefaultMultipartMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(MultipartHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #3
Source File: ClientCodecConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(12, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); // SPR-16804
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertSseReader(readers);
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #4
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 #5
Source File: CodecConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(11, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #6
Source File: ClientCodecConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(12, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); // SPR-16804
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertSseReader(readers);
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #7
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 #8
Source File: ServerCodecConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void defaultReaders() {
	List<HttpMessageReader<?>> readers = this.configurer.getReaders();
	assertEquals(13, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), true);
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(SynchronossPartHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(MultipartHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertStringDecoder(getNextDecoder(readers), false);
}
 
Example #9
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 #10
Source File: CodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomReaders() {
	Decoder<?> customDecoder1 = mock(Decoder.class);
	Decoder<?> customDecoder2 = mock(Decoder.class);

	when(customDecoder1.canDecode(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customDecoder2.canDecode(ResolvableType.forClass(Object.class), null)).thenReturn(true);

	HttpMessageReader<?> customReader1 = mock(HttpMessageReader.class);
	HttpMessageReader<?> customReader2 = mock(HttpMessageReader.class);

	when(customReader1.canRead(ResolvableType.forClass(Object.class), null)).thenReturn(false);
	when(customReader2.canRead(ResolvableType.forClass(Object.class), null)).thenReturn(true);

	this.configurer.customCodecs().decoder(customDecoder1);
	this.configurer.customCodecs().decoder(customDecoder2);

	this.configurer.customCodecs().reader(customReader1);
	this.configurer.customCodecs().reader(customReader2);

	List<HttpMessageReader<?>> readers = this.configurer.getReaders();

	assertEquals(15, readers.size());
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertSame(customDecoder1, getNextDecoder(readers));
	assertSame(customReader1, readers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertSame(customDecoder2, getNextDecoder(readers));
	assertSame(customReader2, readers.get(this.index.getAndIncrement()));
	assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
}
 
Example #11
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 #12
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 #13
Source File: ReactiveJsonParser.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();

    decoder
        .decode(DataBufferUtils.readAsynchronousFileChannel(
            () -> AsynchronousFileChannel.open(Paths.get(ClassLoader.getSystemResource("LargeJsonFile.json").getPath()), StandardOpenOption.READ),
            new DefaultDataBufferFactory(),
            10
        ).delayElements(Duration.ofMillis(100)),
                ResolvableType.forClass(Message.class),
                null, null
        )
        .blockLast();
}
 
Example #14
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 #15
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 #16
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 #17
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 #18
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
	messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
	messageReaders.add(new FormHttpMessageReader());
	SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
	messageReaders.add(partReader);
	messageReaders.add(new MultipartHttpMessageReader(partReader));

	messageReaders.add(new FormHttpMessageReader());

	this.context = new BodyExtractor.Context() {
		@Override
		public List<HttpMessageReader<?>> messageReaders() {
			return messageReaders;
		}

		@Override
		public Optional<ServerHttpResponse> serverResponse() {
			return serverResponse;
		}

		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<String, Object>();
}
 
Example #19
Source File: ClientCodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertSseReader(List<HttpMessageReader<?>> readers) {
	HttpMessageReader<?> reader = readers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageReader.class, reader.getClass());
	Decoder<?> decoder = ((ServerSentEventHttpMessageReader) reader).getDecoder();
	assertNotNull(decoder);
	assertEquals(Jackson2JsonDecoder.class, decoder.getClass());
}
 
Example #20
Source File: ClientCodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
	this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);

	assertSame(decoder, this.configurer.getReaders().stream()
			.filter(reader -> ServerSentEventHttpMessageReader.class.equals(reader.getClass()))
			.map(reader -> (ServerSentEventHttpMessageReader) reader)
			.findFirst()
			.map(ServerSentEventHttpMessageReader::getDecoder)
			.filter(e -> e == decoder).orElse(null));
}
 
Example #21
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
	messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
	messageReaders.add(new FormHttpMessageReader());
	SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
	messageReaders.add(partReader);
	messageReaders.add(new MultipartHttpMessageReader(partReader));

	messageReaders.add(new FormHttpMessageReader());

	this.context = new BodyExtractor.Context() {
		@Override
		public List<HttpMessageReader<?>> messageReaders() {
			return messageReaders;
		}

		@Override
		public Optional<ServerHttpResponse> serverResponse() {
			return serverResponse;
		}

		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<String, Object>();
}
 
Example #22
Source File: AdminApplicationDiscoveryTest.java    From Moss 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 #23
Source File: AbstractAdminApplicationTest.java    From Moss 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 #24
Source File: CodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void defaultAndCustomReaders() {
	Decoder<?> customDecoder1 = mock(Decoder.class);
	Decoder<?> customDecoder2 = mock(Decoder.class);

	given(customDecoder1.canDecode(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customDecoder2.canDecode(ResolvableType.forClass(Object.class), null)).willReturn(true);

	HttpMessageReader<?> customReader1 = mock(HttpMessageReader.class);
	HttpMessageReader<?> customReader2 = mock(HttpMessageReader.class);

	given(customReader1.canRead(ResolvableType.forClass(Object.class), null)).willReturn(false);
	given(customReader2.canRead(ResolvableType.forClass(Object.class), null)).willReturn(true);

	this.configurer.customCodecs().decoder(customDecoder1);
	this.configurer.customCodecs().decoder(customDecoder2);

	this.configurer.customCodecs().reader(customReader1);
	this.configurer.customCodecs().reader(customReader2);

	List<HttpMessageReader<?>> readers = this.configurer.getReaders();

	assertEquals(15, readers.size());
	assertSame(customDecoder1, getNextDecoder(readers));
	assertSame(customReader1, readers.get(this.index.getAndIncrement()));
	assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
	assertSame(customDecoder2, getNextDecoder(readers));
	assertSame(customReader2, readers.get(this.index.getAndIncrement()));
	assertEquals(Jackson2JsonDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jackson2SmileDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(Jaxb2XmlDecoder.class, getNextDecoder(readers).getClass());
	assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
}
 
Example #25
Source File: ClientCodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertSseReader(List<HttpMessageReader<?>> readers) {
	HttpMessageReader<?> reader = readers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageReader.class, reader.getClass());
	Decoder<?> decoder = ((ServerSentEventHttpMessageReader) reader).getDecoder();
	assertNotNull(decoder);
	assertEquals(Jackson2JsonDecoder.class, decoder.getClass());
}
 
Example #26
Source File: ClientCodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
	this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);

	assertSame(decoder, this.configurer.getReaders().stream()
			.filter(reader -> ServerSentEventHttpMessageReader.class.equals(reader.getClass()))
			.map(reader -> (ServerSentEventHttpMessageReader) reader)
			.findFirst()
			.map(ServerSentEventHttpMessageReader::getDecoder)
			.filter(e -> e == decoder).orElse(null));
}
 
Example #27
Source File: BaseDefaultCodecs.java    From java-technology-stack with MIT License 4 votes vote down vote up
protected Decoder<?> getJackson2JsonDecoder() {
	return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder());
}
 
Example #28
Source File: AdviceTraitTesting.java    From problem-spring-web with MIT License 4 votes vote down vote up
default void configureJson(CodecConfigurer configurer) {
    final ObjectMapper mapper = mapper();
    CodecConfigurer.DefaultCodecs defaults = configurer.defaultCodecs();
    defaults.jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
    defaults.jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
}
 
Example #29
Source File: BaseDefaultCodecs.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected Decoder<?> getJackson2JsonDecoder() {
	return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder());
}