diff --git a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java index 6c837376..06c4aa2d 100644 --- a/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java +++ b/src/main/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -15,6 +15,8 @@ */ package org.springframework.hateoas.mvc; +import java.lang.reflect.Type; + import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.Assert; @@ -24,7 +26,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; /** * Extension of {@link MappingJackson2HttpMessageConverter} to constrain the ability to read and write HTTP message * based on the target type. Useful in case the {@link ObjectMapper} about to be configured has customizations that - * sholny be applied to object trees of a certain base type. + * shall only be applied to object trees of a certain base type. * * @author Oliver Gierke */ @@ -49,7 +51,21 @@ public class TypeConstrainedMappingJackson2HttpMessageConverter extends MappingJ */ @Override public boolean canRead(Class clazz, MediaType mediaType) { - return type.isAssignableFrom(clazz) && super.canRead(clazz, mediaType); + return type.isAssignableFrom(clazz) && super.canRead(mediaType); + } + + /* + * (non-Javadoc) + * @see org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#canRead(java.lang.reflect.Type, java.lang.Class, org.springframework.http.MediaType) + */ + @Override + public boolean canRead(Type type, Class contextClass, MediaType mediaType) { + + if (type instanceof Class) { + return canRead((Class) type, mediaType); + } + + return super.canRead(type, contextClass, mediaType); } /* diff --git a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java index 636fe979..5c3698ff 100644 --- a/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverterUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -22,6 +22,7 @@ import org.junit.Test; import org.springframework.hateoas.Resource; import org.springframework.hateoas.ResourceSupport; import org.springframework.http.MediaType; +import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; /** @@ -66,4 +67,16 @@ public class TypeConstrainedMappingJackson2HttpMessageConverterUnitTest { assertThat(converter.canWrite(ResourceSupport.class, MediaType.APPLICATION_JSON), is(true)); assertThat(converter.canWrite(Resource.class, MediaType.APPLICATION_JSON), is(true)); } + + /** + * @see #360 + */ + @Test + public void doesNotSupportAnythingButTheConfiguredClassForCanReadWithContextClass() { + + GenericHttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter( + ResourceSupport.class); + + assertThat(converter.canRead(String.class, Object.class, MediaType.APPLICATION_JSON), is(false)); + } }