Jackson-based message converters consistently check media type first
Issue: SPR-14163
This commit is contained in:
		
							parent
							
								
									416a24c6bc
								
							
						
					
					
						commit
						334b4a99b0
					
				| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -142,20 +142,20 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
 | 
			
		|||
 | 
			
		||||
	@Override
 | 
			
		||||
	protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
 | 
			
		||||
		if (targetClass == null) {
 | 
			
		||||
		if (targetClass == null || !supportsMimeType(message.getHeaders())) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		JavaType javaType = this.objectMapper.constructType(targetClass);
 | 
			
		||||
		if (!jackson23Available || !logger.isWarnEnabled()) {
 | 
			
		||||
			return (this.objectMapper.canDeserialize(javaType) && supportsMimeType(message.getHeaders()));
 | 
			
		||||
			return this.objectMapper.canDeserialize(javaType);
 | 
			
		||||
		}
 | 
			
		||||
		AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
 | 
			
		||||
		if (this.objectMapper.canDeserialize(javaType, causeRef) && supportsMimeType(message.getHeaders())) {
 | 
			
		||||
		if (this.objectMapper.canDeserialize(javaType, causeRef)) {
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
		Throwable cause = causeRef.get();
 | 
			
		||||
		if (cause != null) {
 | 
			
		||||
			String msg = "Failed to evaluate deserialization for type " + javaType;
 | 
			
		||||
			String msg = "Failed to evaluate Jackson deserialization for type " + javaType;
 | 
			
		||||
			if (logger.isDebugEnabled()) {
 | 
			
		||||
				logger.warn(msg, cause);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -168,16 +168,19 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
 | 
			
		|||
 | 
			
		||||
	@Override
 | 
			
		||||
	protected boolean canConvertTo(Object payload, MessageHeaders headers) {
 | 
			
		||||
		if (payload == null || !supportsMimeType(headers)) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		if (!jackson23Available || !logger.isWarnEnabled()) {
 | 
			
		||||
			return (this.objectMapper.canSerialize(payload.getClass()) && supportsMimeType(headers));
 | 
			
		||||
			return this.objectMapper.canSerialize(payload.getClass());
 | 
			
		||||
		}
 | 
			
		||||
		AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
 | 
			
		||||
		if (this.objectMapper.canSerialize(payload.getClass(), causeRef) && supportsMimeType(headers)) {
 | 
			
		||||
		if (this.objectMapper.canSerialize(payload.getClass(), causeRef)) {
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
		Throwable cause = causeRef.get();
 | 
			
		||||
		if (cause != null) {
 | 
			
		||||
			String msg = "Failed to evaluate serialization for type [" + payload.getClass() + "]";
 | 
			
		||||
			String msg = "Failed to evaluate Jackson serialization for type [" + payload.getClass() + "]";
 | 
			
		||||
			if (logger.isDebugEnabled()) {
 | 
			
		||||
				logger.warn(msg, cause);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -142,10 +142,10 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
 | 
			
		|||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
 | 
			
		||||
		JavaType javaType = getJavaType(type, contextClass);
 | 
			
		||||
		if (!canRead(mediaType)) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		JavaType javaType = getJavaType(type, contextClass);
 | 
			
		||||
		if (!jackson23Available || !logger.isWarnEnabled()) {
 | 
			
		||||
			return this.objectMapper.canDeserialize(javaType);
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -155,7 +155,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
 | 
			
		|||
		}
 | 
			
		||||
		Throwable cause = causeRef.get();
 | 
			
		||||
		if (cause != null) {
 | 
			
		||||
			String msg = "Failed to evaluate deserialization for type " + javaType;
 | 
			
		||||
			String msg = "Failed to evaluate Jackson deserialization for type " + javaType;
 | 
			
		||||
			if (logger.isDebugEnabled()) {
 | 
			
		||||
				logger.warn(msg, cause);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -180,7 +180,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
 | 
			
		|||
		}
 | 
			
		||||
		Throwable cause = causeRef.get();
 | 
			
		||||
		if (cause != null) {
 | 
			
		||||
			String msg = "Failed to evaluate serialization for type [" + clazz + "]";
 | 
			
		||||
			String msg = "Failed to evaluate Jackson serialization for type [" + clazz + "]";
 | 
			
		||||
			if (logger.isDebugEnabled()) {
 | 
			
		||||
				logger.warn(msg, cause);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2002-2015 the original author or authors.
 | 
			
		||||
 * Copyright 2002-2016 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -31,7 +31,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		|||
import com.fasterxml.jackson.databind.ser.FilterProvider;
 | 
			
		||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
 | 
			
		||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import org.springframework.core.ParameterizedTypeReference;
 | 
			
		||||
| 
						 | 
				
			
			@ -68,9 +67,7 @@ public class MappingJackson2HttpMessageConverterTests {
 | 
			
		|||
		assertTrue(converter.canWrite(Map.class, new MediaType("application", "json")));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// SPR-7905
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	@Test  // SPR-7905
 | 
			
		||||
	public void canReadAndWriteMicroformats() {
 | 
			
		||||
		assertTrue(converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json")));
 | 
			
		||||
		assertTrue(converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json")));
 | 
			
		||||
| 
						 | 
				
			
			@ -439,9 +436,12 @@ public class MappingJackson2HttpMessageConverterTests {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	private interface MyJacksonView1 {};
 | 
			
		||||
 | 
			
		||||
	private interface MyJacksonView2 {};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@SuppressWarnings("unused")
 | 
			
		||||
	private static class JacksonViewBean {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -478,11 +478,13 @@ public class MappingJackson2HttpMessageConverterTests {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@JsonFilter("myJacksonFilter")
 | 
			
		||||
	@SuppressWarnings("unused")
 | 
			
		||||
	private static class JacksonFilteredBean {
 | 
			
		||||
 | 
			
		||||
		private String property1;
 | 
			
		||||
 | 
			
		||||
		private String property2;
 | 
			
		||||
 | 
			
		||||
		public String getProperty1() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue