|
1 | 1 | /* |
2 | | - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 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.io.IOException; |
21 | 21 | import java.io.StringWriter; |
22 | 22 | import java.io.Writer; |
| 23 | +import java.lang.reflect.Type; |
23 | 24 | import java.nio.charset.Charset; |
24 | 25 | import java.util.Arrays; |
25 | 26 | import java.util.concurrent.atomic.AtomicReference; |
|
30 | 31 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
31 | 32 | import com.fasterxml.jackson.databind.DeserializationFeature; |
32 | 33 | import com.fasterxml.jackson.databind.JavaType; |
| 34 | +import com.fasterxml.jackson.databind.JsonMappingException; |
33 | 35 | import com.fasterxml.jackson.databind.MapperFeature; |
34 | 36 | import com.fasterxml.jackson.databind.ObjectMapper; |
35 | 37 | import com.fasterxml.jackson.databind.SerializationFeature; |
@@ -136,50 +138,56 @@ private void configurePrettyPrint() { |
136 | 138 |
|
137 | 139 | @Override |
138 | 140 | protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { |
139 | | - if (targetClass == null) { |
| 141 | + if (targetClass == null || !supportsMimeType(message.getHeaders())) { |
140 | 142 | return false; |
141 | 143 | } |
142 | 144 | JavaType javaType = this.objectMapper.constructType(targetClass); |
143 | 145 | if (!logger.isWarnEnabled()) { |
144 | | - return (this.objectMapper.canDeserialize(javaType) && supportsMimeType(message.getHeaders())); |
| 146 | + return this.objectMapper.canDeserialize(javaType); |
145 | 147 | } |
146 | 148 | AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>(); |
147 | | - if (this.objectMapper.canDeserialize(javaType, causeRef) && supportsMimeType(message.getHeaders())) { |
| 149 | + if (this.objectMapper.canDeserialize(javaType, causeRef)) { |
148 | 150 | return true; |
149 | 151 | } |
150 | | - Throwable cause = causeRef.get(); |
151 | | - if (cause != null) { |
152 | | - String msg = "Failed to evaluate deserialization for type " + javaType; |
153 | | - if (logger.isDebugEnabled()) { |
154 | | - logger.warn(msg, cause); |
155 | | - } |
156 | | - else { |
157 | | - logger.warn(msg + ": " + cause); |
158 | | - } |
159 | | - } |
| 152 | + logWarningIfNecessary(javaType, causeRef.get()); |
160 | 153 | return false; |
161 | 154 | } |
162 | 155 |
|
163 | 156 | @Override |
164 | 157 | protected boolean canConvertTo(Object payload, MessageHeaders headers) { |
| 158 | + if (payload == null || !supportsMimeType(headers)) { |
| 159 | + return false; |
| 160 | + } |
165 | 161 | if (!logger.isWarnEnabled()) { |
166 | | - return (this.objectMapper.canSerialize(payload.getClass()) && supportsMimeType(headers)); |
| 162 | + return this.objectMapper.canSerialize(payload.getClass()); |
167 | 163 | } |
168 | 164 | AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>(); |
169 | | - if (this.objectMapper.canSerialize(payload.getClass(), causeRef) && supportsMimeType(headers)) { |
| 165 | + if (this.objectMapper.canSerialize(payload.getClass(), causeRef)) { |
170 | 166 | return true; |
171 | 167 | } |
172 | | - Throwable cause = causeRef.get(); |
173 | | - if (cause != null) { |
174 | | - String msg = "Failed to evaluate serialization for type [" + payload.getClass() + "]"; |
| 168 | + logWarningIfNecessary(payload.getClass(), causeRef.get()); |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Determine whether to log the given exception coming from a |
| 174 | + * {@link ObjectMapper#canDeserialize} / {@link ObjectMapper#canSerialize} check. |
| 175 | + * @param type the class that Jackson tested for (de-)serializability |
| 176 | + * @param cause the Jackson-thrown exception to evaluate |
| 177 | + * (typically a {@link JsonMappingException}) |
| 178 | + * @since 4.3 |
| 179 | + */ |
| 180 | + protected void logWarningIfNecessary(Type type, Throwable cause) { |
| 181 | + if (cause != null && !(cause instanceof JsonMappingException && cause.getMessage().startsWith("Can not find"))) { |
| 182 | + String msg = "Failed to evaluate Jackson " + (type instanceof JavaType ? "de" : "") + |
| 183 | + "serialization for type [" + type + "]"; |
175 | 184 | if (logger.isDebugEnabled()) { |
176 | 185 | logger.warn(msg, cause); |
177 | 186 | } |
178 | 187 | else { |
179 | 188 | logger.warn(msg + ": " + cause); |
180 | 189 | } |
181 | 190 | } |
182 | | - return false; |
183 | 191 | } |
184 | 192 |
|
185 | 193 | @Override |
|
0 commit comments