diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java index c91c56a97c..b9471e1370 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java @@ -143,28 +143,30 @@ public abstract class HandlerResultHandlerSupport implements Ordered { List result = new ArrayList<>(compatibleMediaTypes); MediaType.sortBySpecificityAndQuality(result); + MediaType selected = null; for (MediaType mediaType : result) { if (mediaType.isConcrete()) { - if (logger.isDebugEnabled()) { - logger.debug(exchange.getLogPrefix() + "Using '" + mediaType + "' given " + acceptableTypes); - } - return mediaType; + selected = mediaType; + break; } else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) { - mediaType = MediaType.APPLICATION_OCTET_STREAM; - if (logger.isDebugEnabled()) { - logger.debug(exchange.getLogPrefix() + "Using '" + mediaType + "' given " + acceptableTypes); - } - return mediaType; + selected = MediaType.APPLICATION_OCTET_STREAM; + break; } } - if (logger.isDebugEnabled()) { + if (selected != null) { + if (logger.isDebugEnabled()) { + logger.debug("Using '" + selected + "' given " + + acceptableTypes + " and supported " + producibleTypes); + } + } + else if (logger.isDebugEnabled()) { logger.debug(exchange.getLogPrefix() + "No match for " + acceptableTypes + ", supported: " + producibleTypes); } - return null; + return selected; } private List getAcceptableTypes(ServerWebExchange exchange) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java index e1a50eed7f..1c5c90be22 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java @@ -214,59 +214,58 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe } } - - List mediaTypesToUse; - + MediaType selectedMediaType = null; MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType != null && contentType.isConcrete()) { if (logger.isDebugEnabled()) { logger.debug("Found 'Content-Type:" + contentType + "' in response"); } - mediaTypesToUse = Collections.singletonList(contentType); + selectedMediaType = contentType; } else { HttpServletRequest request = inputMessage.getServletRequest(); - List requestedMediaTypes = getAcceptableMediaTypes(request); - List producibleMediaTypes = getProducibleMediaTypes(request, valueType, targetType); + List acceptableTypes = getAcceptableMediaTypes(request); + List producibleTypes = getProducibleMediaTypes(request, valueType, targetType); - if (body != null && producibleMediaTypes.isEmpty()) { + if (body != null && producibleTypes.isEmpty()) { throw new HttpMessageNotWritableException( "No converter found for return value of type: " + valueType); } - mediaTypesToUse = new ArrayList<>(); - for (MediaType requestedType : requestedMediaTypes) { - for (MediaType producibleType : producibleMediaTypes) { + List mediaTypesToUse = new ArrayList<>(); + for (MediaType requestedType : acceptableTypes) { + for (MediaType producibleType : producibleTypes) { if (requestedType.isCompatibleWith(producibleType)) { mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType)); } } } - if (logger.isDebugEnabled()) { - logger.debug("No match for " + requestedMediaTypes + ", supported: " + producibleMediaTypes); - } if (mediaTypesToUse.isEmpty()) { if (body != null) { - throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes); + throw new HttpMediaTypeNotAcceptableException(producibleTypes); + } + if (logger.isDebugEnabled()) { + logger.debug("No match for " + acceptableTypes + ", supported: " + producibleTypes); } return; } + MediaType.sortBySpecificityAndQuality(mediaTypesToUse); - } - MediaType selectedMediaType = null; - for (MediaType mediaType : mediaTypesToUse) { - if (mediaType.isConcrete()) { - selectedMediaType = mediaType; - break; - } - else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) { - selectedMediaType = MediaType.APPLICATION_OCTET_STREAM; - break; + for (MediaType mediaType : mediaTypesToUse) { + if (mediaType.isConcrete()) { + selectedMediaType = mediaType; + break; + } + else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) { + selectedMediaType = MediaType.APPLICATION_OCTET_STREAM; + break; + } } - } - if (logger.isDebugEnabled()) { - logger.debug("Using '" + selectedMediaType + "' given " + mediaTypesToUse); + if (logger.isDebugEnabled()) { + logger.debug("Using '" + selectedMediaType + "', given " + + acceptableTypes + " and supported " + producibleTypes); + } } if (selectedMediaType != null) {