GH-3483: Fallback to RestTemplate.converters (#3485)
* GH-3483: Fallback to RestTemplate.converters Fixes https://github.com/spring-projects/spring-integration/issues/3483 When there is no reasonable way to determine a `Content-Type` from the request message, do not set an `application/x-java-serialized-object` as a fallback and let the `RestTemplate` to determine the target type and conversion through its `HttpMessageConverter` set * Remove `application/x-java-serialized-object` fallback from the `AbstractHttpRequestExecutingMessageHandler` * Adjust its log messages according `LogAccessor` * Un`@Disable` `WebFluxDslTests` since fix was done in Spring Security * Add `HttpRequestExecutingMessageHandlerTests.testNoContentTypeAndSmartConverter()` * Mention change in `What's New` * Fix language in whats-new.adoc Co-authored-by: Gary Russell <grussell@vmware.com> Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,6 +27,7 @@ import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -71,6 +72,9 @@ import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||
|
||||
import reactor.core.publisher.Sinks;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -178,8 +182,7 @@ public class HttpRequestExecutingMessageHandlerTests {
|
||||
assertThat(map.get(1).toString()).isEqualTo("Philadelphia");
|
||||
assertThat(map.get(2).toString()).isEqualTo("Ambler");
|
||||
assertThat(map.get(3).toString()).isEqualTo("Mohnton");
|
||||
assertThat(request.getHeaders().getContentType().getType()).isEqualTo("application");
|
||||
assertThat(request.getHeaders().getContentType().getSubtype()).isEqualTo("x-java-serialized-object");
|
||||
assertThat(request.getHeaders().getContentType()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -817,6 +820,43 @@ public class HttpRequestExecutingMessageHandlerTests {
|
||||
assertThat(accept.get(0).getSubtype()).isEqualTo("x-java-serialized-object");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoContentTypeAndSmartConverter() throws IOException {
|
||||
Sinks.One<HttpHeaders> httpHeadersSink = Sinks.one();
|
||||
RestTemplate testRestTemplate = new RestTemplate() {
|
||||
@Nullable
|
||||
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
|
||||
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
|
||||
|
||||
try {
|
||||
ClientHttpRequest request = createRequest(url, method);
|
||||
requestCallback.doWithRequest(request);
|
||||
httpHeadersSink.tryEmitValue(request.getHeaders());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RestClientException("Not possible", e);
|
||||
}
|
||||
throw new RuntimeException("intentional");
|
||||
}
|
||||
};
|
||||
|
||||
HttpRequestExecutingMessageHandler handler =
|
||||
new HttpRequestExecutingMessageHandler("https://www.springsource.org/spring-integration",
|
||||
testRestTemplate);
|
||||
setBeanFactory(handler);
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> handler.handleMessage(new GenericMessage<>(new City("London"))));
|
||||
|
||||
StepVerifier.create(httpHeadersSink.asMono())
|
||||
.assertNext(headers ->
|
||||
assertThat(headers)
|
||||
.containsEntry(HttpHeaders.CONTENT_TYPE,
|
||||
Arrays.asList(MediaType.APPLICATION_JSON_VALUE)))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
private void setBeanFactory(HttpRequestExecutingMessageHandler handler) {
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
}
|
||||
@@ -854,6 +894,10 @@ public class HttpRequestExecutingMessageHandlerTests {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
|
||||
Reference in New Issue
Block a user