From 0b4cd61c8b05bdea1e59e777ce8fe9689c0f78b2 Mon Sep 17 00:00:00 2001 From: Igor Murzich Date: Wed, 8 Oct 2025 17:39:58 +0300 Subject: [PATCH 1/2] Keep order of produce media types in Exeception handler Signed-off-by: Igor Murzich --- .../web/method/annotation/ExceptionHandlerMethodResolver.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java index 05698967e370..45e9470bc8f9 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java @@ -21,7 +21,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -121,7 +121,7 @@ private ExceptionHandlerMappingInfo detectExceptionMappings(Method method) { if (exceptions.isEmpty()) { throw new IllegalStateException("No exception types mapped to " + method); } - Set mediaTypes = new HashSet<>(); + Set mediaTypes = new LinkedHashSet<>(); for (String mediaType : exceptionHandler.produces()) { try { mediaTypes.add(MediaType.parseMediaType(mediaType)); From 605776323812ebaef636883a495cdbd475ce9fbf Mon Sep 17 00:00:00 2001 From: Igor Murzich Date: Wed, 8 Oct 2025 18:35:39 +0300 Subject: [PATCH 2/2] Add test shouldKeepProduceMediaTypesOrder Signed-off-by: Igor Murzich --- .../annotation/ExceptionHandlerMethodResolverTests.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java index a99eb76ced06..aa9a95301605 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java @@ -117,6 +117,13 @@ void shouldResolveMethodWithMediaType() { assertThat(resolver.resolveExceptionMapping(new IllegalArgumentException(), MediaType.TEXT_HTML).getHandlerMethod().getName()).isEqualTo("handleHtml"); } + @Test + void shouldKeepProduceMediaTypesOrder() { + ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(MediaTypeController.class); + assertThat(resolver.resolveExceptionMapping(new IllegalArgumentException(), MediaType.TEXT_HTML).getProducibleTypes().toString()).isEqualTo("[text/html, */*]"); + } + + @Test void shouldResolveMethodWithCompatibleMediaType() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(MediaTypeController.class);