diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractGenericHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractGenericHttpMessageConverter.java index a52b032acaf..a15d0468d8b 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/AbstractGenericHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractGenericHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -29,6 +29,7 @@ import org.springframework.http.StreamingHttpOutputMessage; * Abstract base class for most {@link GenericHttpMessageConverter} implementations. * * @author Sebastien Deleuze + * @author Juergen Hoeller * @since 4.2 */ public abstract class AbstractGenericHttpMessageConverter extends AbstractHttpMessageConverter @@ -58,9 +59,14 @@ public abstract class AbstractGenericHttpMessageConverter extends AbstractHtt } + @Override + protected boolean supports(Class clazz) { + return true; + } + @Override public boolean canRead(Type type, Class contextClass, MediaType mediaType) { - return canRead(contextClass, mediaType); + return (type instanceof Class ? canRead((Class) type, mediaType) : canRead(mediaType)); } @Override @@ -102,7 +108,6 @@ public abstract class AbstractGenericHttpMessageConverter extends AbstractHtt } } - @Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { @@ -113,7 +118,7 @@ public abstract class AbstractGenericHttpMessageConverter extends AbstractHtt /** * Abstract template method that writes the actual body. Invoked from {@link #write}. * @param t the object to write to the output message - * @param type the type of object to write, can be {@code null} if not specified. + * @param type the type of object to write (may be {@code null}) * @param outputMessage the HTTP output message to write to * @throws IOException in case of I/O errors * @throws HttpMessageNotWritableException in case of conversion errors diff --git a/spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java index 6b371f16ecf..e90722eea63 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java @@ -51,8 +51,24 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa @Override - protected boolean supports(Class clazz) { - // should not be called as we override canRead/canWrite + @SuppressWarnings("unchecked") + protected MediaType getDefaultContentType(Object object) { + if (jafPresent) { + if(object instanceof ResourceRegion) { + return ActivationMediaTypeFactory.getMediaType(((ResourceRegion) object).getResource()); + } + else { + Collection regions = (Collection) object; + if(regions.size() > 0) { + return ActivationMediaTypeFactory.getMediaType(regions.iterator().next().getResource()); + } + } + } + return MediaType.APPLICATION_OCTET_STREAM; + } + + @Override + public boolean canRead(Class clazz, MediaType mediaType) { return false; } @@ -65,31 +81,14 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - return null; + throw new UnsupportedOperationException(); } @Override protected ResourceRegion readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - return null; - } - - @Override - @SuppressWarnings("unchecked") - protected MediaType getDefaultContentType(Object object) { - if (jafPresent) { - if(object instanceof ResourceRegion) { - return ActivationMediaTypeFactory.getMediaType(((ResourceRegion) object).getResource()); - } - else { - Collection regions = (Collection) object; - if(regions.size() > 0) { - return ActivationMediaTypeFactory.getMediaType(regions.iterator().next().getResource()); - } - } - } - return MediaType.APPLICATION_OCTET_STREAM; + throw new UnsupportedOperationException(); } @Override @@ -100,7 +99,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa @Override public boolean canWrite(Type type, Class clazz, MediaType mediaType) { if (!(type instanceof ParameterizedType)) { - return ResourceRegion.class.isAssignableFrom((Class) type); + return ResourceRegion.class.isAssignableFrom((Class) type); } ParameterizedType parameterizedType = (ParameterizedType) type; if (!(parameterizedType.getRawType() instanceof Class)) { @@ -140,6 +139,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa } } + protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException { Assert.notNull(region, "ResourceRegion must not be null"); HttpHeaders responseHeaders = outputMessage.getHeaders(); @@ -197,8 +197,6 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa print(out, "--" + boundaryString + "--"); } - - private static void println(OutputStream os) throws IOException { os.write('\r'); os.write('\n'); diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index f14c82737a5..503902aba27 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -203,12 +203,6 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener } } - @Override - protected boolean supports(Class clazz) { - // should not be called, since we override canRead/Write instead - throw new UnsupportedOperationException(); - } - @Override protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { @@ -237,7 +231,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener return this.objectMapper.readValue(inputMessage.getBody(), javaType); } catch (IOException ex) { - throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex); + throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex); } } @@ -289,7 +283,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener } catch (JsonProcessingException ex) { - throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex); + throw new HttpMessageNotWritableException("Could not write JSON document: " + ex.getMessage(), ex); } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java index d82bd28d452..2ae751793ca 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -114,34 +114,20 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte @Override - public boolean canRead(Class clazz, MediaType mediaType) { - return canRead(mediaType); - } - - @Override - public boolean canWrite(Class clazz, MediaType mediaType) { - return canWrite(mediaType); - } - - @Override - protected boolean supports(Class clazz) { - // should not be called, since we override canRead/Write instead - throw new UnsupportedOperationException(); - } - - @Override - protected Object readInternal(Class clazz, HttpInputMessage inputMessage) + @SuppressWarnings("deprecation") + public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - TypeToken token = getTypeToken(clazz); + TypeToken token = getTypeToken(type); return readTypeToken(token, inputMessage); } @Override - public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) + @SuppressWarnings("deprecation") + protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - TypeToken token = getTypeToken(type); + TypeToken token = getTypeToken(clazz); return readTypeToken(token, inputMessage); } @@ -162,7 +148,9 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte * * @param type the type for which to return the TypeToken * @return the type token + * @deprecated as of Spring Framework 4.3.8, in favor of signature-based resolution */ + @Deprecated protected TypeToken getTypeToken(Type type) { return TypeToken.get(type); } @@ -173,7 +161,7 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte return this.gson.fromJson(json, token.getType()); } catch (JsonParseException ex) { - throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); + throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex); } } @@ -203,7 +191,7 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte writer.close(); } catch (JsonIOException ex) { - throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); + throw new HttpMessageNotWritableException("Could not write JSON document: " + ex.getMessage(), ex); } }