Skip to content

Commit 4d8b05a

Browse files
committed
#867 - Only disable default encoders and decoders when using Spring 5.1.
Spring Framework 5.1 puts custom encoders and decoders in the wrong place, so Spring HATEOAS can't properly register with WebFlux. To make Spring HATEOS media types work requires overriding features that break other toolkits, like Spring Cloud Sleuth. However, in Spring Framework 5.2, this is no longer needed, so we need only disable the registry when Spring Framework 5.1 is on the classpath. Related issues: #885, spring-projects/spring-framework#22612
1 parent c4805ae commit 4d8b05a

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

src/main/java/org/springframework/hateoas/config/WebClientConfigurer.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package org.springframework.hateoas.config;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import lombok.RequiredArgsConstructor;
4-
5-
import java.util.ArrayList;
6-
import java.util.Collection;
7-
import java.util.List;
8-
95
import org.springframework.context.annotation.Configuration;
106
import org.springframework.core.codec.CharSequenceEncoder;
117
import org.springframework.core.codec.Decoder;
128
import org.springframework.core.codec.Encoder;
139
import org.springframework.core.codec.StringDecoder;
1410
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
11+
import org.springframework.hateoas.support.SpringUtilities;
1512
import org.springframework.http.codec.json.Jackson2JsonDecoder;
1613
import org.springframework.http.codec.json.Jackson2JsonEncoder;
1714
import org.springframework.util.MimeType;
1815
import org.springframework.web.reactive.function.client.ExchangeStrategies;
1916
import org.springframework.web.reactive.function.client.WebClient;
2017

21-
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.List;
2221

2322
/**
2423
* Assembles {@link ExchangeStrategies} needed to wire a {@link WebClient} with hypermedia support.
@@ -52,15 +51,21 @@ public ExchangeStrategies hypermediaExchangeStrategies() {
5251
decoders.add(new Jackson2JsonDecoder(objectMapper, mimeTypes));
5352
});
5453

55-
encoders.add(CharSequenceEncoder.allMimeTypes());
56-
decoders.add(StringDecoder.allMimeTypes());
54+
if (SpringUtilities.isSpring5_1()) {
55+
56+
encoders.add(CharSequenceEncoder.allMimeTypes());
57+
decoders.add(StringDecoder.allMimeTypes());
58+
}
5759

5860
return ExchangeStrategies.builder().codecs(clientCodecConfigurer -> {
5961

6062
encoders.forEach(encoder -> clientCodecConfigurer.customCodecs().encoder(encoder));
6163
decoders.forEach(decoder -> clientCodecConfigurer.customCodecs().decoder(decoder));
6264

63-
clientCodecConfigurer.registerDefaults(false);
65+
if (SpringUtilities.isSpring5_1()) {
66+
clientCodecConfigurer.registerDefaults(false);
67+
}
68+
6469
}).build();
6570
}
6671

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.hateoas.support;
18+
19+
import lombok.experimental.UtilityClass;
20+
21+
import org.springframework.context.ApplicationContext;
22+
23+
/**
24+
* Utility for Spring-specific functions.
25+
*
26+
* @author Greg Turnquist
27+
*/
28+
@UtilityClass
29+
public class SpringUtilities {
30+
31+
/**
32+
* @return boolean indicating this is Spring Framework 5.1
33+
*/
34+
public boolean isSpring5_1() {
35+
36+
String versionOfSpringFramework = ApplicationContext.class.getPackage().getImplementationVersion();
37+
38+
String[] parts = versionOfSpringFramework.split("\\.");
39+
int majorVersion = Integer.parseInt(parts[0]);
40+
int minorVersion = Integer.parseInt(parts[1]);
41+
42+
return majorVersion == 5 && minorVersion == 1;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)