|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.Collection;
|
22 | 22 | import java.util.Collections;
|
| 23 | +import java.util.HashMap; |
23 | 24 | import java.util.Iterator;
|
24 | 25 | import java.util.List;
|
| 26 | +import java.util.Map; |
25 | 27 |
|
26 | 28 | import org.springframework.http.converter.HttpMessageConverter;
|
27 | 29 | import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
@@ -63,6 +65,15 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
|
63 | 65 | NON_REPLACING_CONVERTERS = Collections.unmodifiableList(nonReplacingConverters);
|
64 | 66 | }
|
65 | 67 |
|
| 68 | + private static final Map<Class<?>, Class<?>> EQUIVALENT_CONVERTERS; |
| 69 | + |
| 70 | + static { |
| 71 | + Map<Class<?>, Class<?>> equivalentConverters = new HashMap<>(); |
| 72 | + putIfExists(equivalentConverters, "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter", |
| 73 | + "org.springframework.http.converter.json.GsonHttpMessageConverter"); |
| 74 | + EQUIVALENT_CONVERTERS = Collections.unmodifiableMap(equivalentConverters); |
| 75 | + } |
| 76 | + |
66 | 77 | private final List<HttpMessageConverter<?>> converters;
|
67 | 78 |
|
68 | 79 | /**
|
@@ -132,7 +143,12 @@ private boolean isReplacement(HttpMessageConverter<?> defaultConverter, HttpMess
|
132 | 143 | return false;
|
133 | 144 | }
|
134 | 145 | }
|
135 |
| - return ClassUtils.isAssignableValue(defaultConverter.getClass(), candidate); |
| 146 | + Class<?> converterClass = defaultConverter.getClass(); |
| 147 | + if (ClassUtils.isAssignableValue(converterClass, candidate)) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + Class<?> equivalentClass = EQUIVALENT_CONVERTERS.get(converterClass); |
| 151 | + return equivalentClass != null && ClassUtils.isAssignableValue(equivalentClass, candidate); |
136 | 152 | }
|
137 | 153 |
|
138 | 154 | private void configurePartConverters(AllEncompassingFormHttpMessageConverter formConverter,
|
@@ -220,4 +236,13 @@ private static void addClassIfExists(List<Class<?>> list, String className) {
|
220 | 236 | }
|
221 | 237 | }
|
222 | 238 |
|
| 239 | + private static void putIfExists(Map<Class<?>, Class<?>> map, String keyClassName, String valueClassName) { |
| 240 | + try { |
| 241 | + map.put(Class.forName(keyClassName), Class.forName(valueClassName)); |
| 242 | + } |
| 243 | + catch (ClassNotFoundException | NoClassDefFoundError ex) { |
| 244 | + // Ignore |
| 245 | + } |
| 246 | + } |
| 247 | + |
223 | 248 | }
|
0 commit comments