diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java index 52a09c4755..6828953245 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java @@ -101,8 +101,9 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv } /** - * Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types} - * include the given media type. + * Returns true if any of the {@linkplain #setSupportedMediaTypes(List) + * supported} media types {@link MediaType#includes(MediaType) include} the + * given media type. * @param mediaType the media type to read, can be {@code null} if not specified. * Typically the value of a {@code Content-Type} header. * @return {@code true} if the supported media types include the media type, @@ -121,8 +122,9 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv } /** - * This implementation checks if the given class is {@linkplain #supports(Class) supported}, - * and if the {@linkplain #getSupportedMediaTypes() supported media types} + * This implementation checks if the given class is + * {@linkplain #supports(Class) supported}, and if the + * {@linkplain #getSupportedMediaTypes() supported} media types * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override diff --git a/spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java index 83c8dc91da..10a52d919d 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-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. @@ -24,43 +24,55 @@ import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Test-case for AbstractHttpMessageConverter. * * @author Arjen Poutsma + * @author Rossen Stoyanchev */ public class HttpMessageConverterTests { - private static final MediaType MEDIA_TYPE = new MediaType("foo", "bar"); @Test public void canRead() { - AbstractHttpMessageConverter converter = new MyHttpMessageConverter(MEDIA_TYPE) { - @Override - protected boolean supports(Class clazz) { - return MyType.class.equals(clazz); - } + MediaType mediaType = new MediaType("foo", "bar"); + HttpMessageConverter converter = new MyHttpMessageConverter(mediaType); - }; - - assertTrue(converter.canRead(MyType.class, MEDIA_TYPE)); + assertTrue(converter.canRead(MyType.class, mediaType)); assertFalse(converter.canRead(MyType.class, new MediaType("foo", "*"))); assertFalse(converter.canRead(MyType.class, MediaType.ALL)); } + @Test + public void canReadWithWildcardSubtype() { + MediaType mediaType = new MediaType("foo"); + HttpMessageConverter converter = new MyHttpMessageConverter(mediaType); + + assertTrue(converter.canRead(MyType.class, new MediaType("foo", "bar"))); + assertTrue(converter.canRead(MyType.class, new MediaType("foo", "*"))); + assertFalse(converter.canRead(MyType.class, MediaType.ALL)); + } + @Test public void canWrite() { - AbstractHttpMessageConverter converter = new MyHttpMessageConverter(MEDIA_TYPE) { - @Override - protected boolean supports(Class clazz) { - return MyType.class.equals(clazz); - } + MediaType mediaType = new MediaType("foo", "bar"); + HttpMessageConverter converter = new MyHttpMessageConverter(mediaType); - }; + assertTrue(converter.canWrite(MyType.class, mediaType)); + assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "*"))); + assertTrue(converter.canWrite(MyType.class, MediaType.ALL)); + } - assertTrue(converter.canWrite(MyType.class, MEDIA_TYPE)); + @Test + public void canWriteWithWildcardInSupportedSubtype() { + MediaType mediaType = new MediaType("foo"); + HttpMessageConverter converter = new MyHttpMessageConverter(mediaType); + + assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "bar"))); assertTrue(converter.canWrite(MyType.class, new MediaType("foo", "*"))); assertTrue(converter.canWrite(MyType.class, MediaType.ALL)); } @@ -74,8 +86,7 @@ public class HttpMessageConverterTests { @Override protected boolean supports(Class clazz) { - fail("Not expected"); - return false; + return MyType.class.equals(clazz); } @Override