From 58bb6e7181adc9671a3f7758dc9785873a497390 Mon Sep 17 00:00:00 2001 From: Christoph Dreis Date: Tue, 1 Nov 2022 20:04:13 +0100 Subject: [PATCH] Reduce allocations caused by producible media types Closes gh-29412 --- .../result/method/RequestMappingInfoHandlerMapping.java | 9 ++++++--- .../mvc/method/RequestMappingInfoHandlerMapping.java | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java index a6a7cbf27d..1716b33eef 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java @@ -141,9 +141,12 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe exchange.getAttributes().put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables); exchange.getAttributes().put(MATRIX_VARIABLES_ATTRIBUTE, matrixVariables); - if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) { - Set mediaTypes = info.getProducesCondition().getProducibleMediaTypes(); - exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes); + ProducesRequestCondition producesCondition = info.getProducesCondition(); + if (!producesCondition.isEmpty()) { + Set mediaTypes = producesCondition.getProducibleMediaTypes(); + if (!mediaTypes.isEmpty()) { + exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes); + } } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index 0e9b4dff1c..3f2a099f00 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -147,9 +147,12 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe extractMatchDetails((PatternsRequestCondition) condition, lookupPath, request); } - if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) { - Set mediaTypes = info.getProducesCondition().getProducibleMediaTypes(); - request.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes); + ProducesRequestCondition producesCondition = info.getProducesCondition(); + if (!producesCondition.isEmpty()) { + Set mediaTypes = producesCondition.getProducibleMediaTypes(); + if (!mediaTypes.isEmpty()) { + request.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes); + } } }