|
58 | 58 | * registered media-type extension (or trailing slash), the suffix-stripped path is matched first so |
59 | 59 | * content negotiation works and generic {@code /{property}} handlers do not swallow names like |
60 | 60 | * {@code metadata.json}. If the stripped lookup finds no handler, or finds one whose {@code |
61 | | - * produces} cannot satisfy the extension-derived media type ({@code 406}), we fall back to the |
62 | | - * original path so controllers that map an extension literally (analytics {@code .xml}/{@code .csv} |
63 | | - * downloads, OpenAPI's {@code /openapi/openapi.json}) keep working. The resolved media type is |
64 | | - * recorded for {@link SuffixMediaTypeContentNegotiationStrategy}. |
| 61 | + * produces} cannot satisfy the extension-derived media type ({@code 406}), we fall back in order: |
| 62 | + * |
| 63 | + * <ol> |
| 64 | + * <li>slash-only strip when both a trailing slash and a registered extension were present (so |
| 65 | + * literal maps like {@code /api/analytics.xlsx} still match {@code /api/analytics.xlsx/}) |
| 66 | + * <li>the original path (literal-extension controllers such as OpenAPI's {@code |
| 67 | + * /openapi/openapi.json}) |
| 68 | + * </ol> |
| 69 | + * |
| 70 | + * Rethrow the 406 only when no fallback mapping exists. The resolved media type is recorded for |
| 71 | + * {@link SuffixMediaTypeContentNegotiationStrategy}. |
65 | 72 | * |
66 | 73 | * <p>Forward-compatible on Spring 6.2 (Spring 7 readiness PR-F). |
67 | 74 | */ |
@@ -98,33 +105,73 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex |
98 | 105 | // controller mappings such as /openapi/openapi.json), or the stripped path matches a handler |
99 | 106 | // whose produces cannot satisfy the extension-derived media type (406 - common for analytics |
100 | 107 | // download endpoints that map .xml/.csv/... literally next to a JSON produces handler), fall |
101 | | - // back to the original path. |
| 108 | + // back toward the original path. When both a trailing slash and an extension were present, |
| 109 | + // try keeping the extension first (slash-only strip) before the raw original URI. |
102 | 110 | // |
103 | 111 | // Paths without a registered extension: match as-is, then fall back to trailing-slash strip. |
104 | 112 | HttpServletRequest normalized = normalize(request); |
105 | | - if (normalized != request) { |
106 | | - clearAndReparsePathCaches(normalized); |
| 113 | + if (normalized == request) { |
| 114 | + return super.getHandlerInternal(request); |
| 115 | + } |
| 116 | + |
| 117 | + HttpMediaTypeNotAcceptableException notAcceptable = null; |
| 118 | + |
| 119 | + clearAndReparsePathCaches(normalized); |
| 120 | + try { |
| 121 | + HandlerMethod stripped = super.getHandlerInternal(normalized); |
| 122 | + if (stripped != null) { |
| 123 | + return stripped; |
| 124 | + } |
| 125 | + } catch (HttpMediaTypeNotAcceptableException ex) { |
| 126 | + // Stripped path matched a handler, but its produces does not include the media type forced |
| 127 | + // by the path extension. Prefer a literal-suffix mapping when one exists; otherwise rethrow |
| 128 | + // so the client still gets 406 rather than a misleading 404. |
| 129 | + notAcceptable = ex; |
| 130 | + } |
| 131 | + |
| 132 | + // /api/analytics.xlsx/ fully strips to /api/analytics (JSON produces → 406). Retry with only |
| 133 | + // the trailing slash removed so the literal /api/analytics.xlsx mapping can still win. |
| 134 | + HttpServletRequest slashOnly = trailingSlashOnly(request); |
| 135 | + if (slashOnly != null) { |
| 136 | + clearAndReparsePathCaches(slashOnly); |
107 | 137 | try { |
108 | | - HandlerMethod stripped = super.getHandlerInternal(normalized); |
109 | | - if (stripped != null) { |
110 | | - return stripped; |
| 138 | + HandlerMethod literalWithExtension = super.getHandlerInternal(slashOnly); |
| 139 | + if (literalWithExtension != null) { |
| 140 | + return literalWithExtension; |
111 | 141 | } |
112 | | - } catch (HttpMediaTypeNotAcceptableException notAcceptable) { |
113 | | - // Stripped path matched a handler, but its produces does not include the media type forced |
114 | | - // by the path extension. Prefer a literal-suffix mapping when one exists; otherwise rethrow |
115 | | - // so the client still gets 406 rather than a misleading 404. |
116 | | - clearAndReparsePathCaches(request); |
117 | | - HandlerMethod literal = super.getHandlerInternal(request); |
118 | | - if (literal != null) { |
119 | | - return literal; |
| 142 | + } catch (HttpMediaTypeNotAcceptableException ex) { |
| 143 | + if (notAcceptable == null) { |
| 144 | + notAcceptable = ex; |
120 | 145 | } |
121 | | - throw notAcceptable; |
122 | 146 | } |
123 | | - // No stripped handler - try the original path (literal extension mappings). |
124 | | - clearAndReparsePathCaches(request); |
125 | 147 | } |
126 | 148 |
|
127 | | - return super.getHandlerInternal(request); |
| 149 | + clearAndReparsePathCaches(request); |
| 150 | + HandlerMethod original = super.getHandlerInternal(request); |
| 151 | + if (original != null) { |
| 152 | + return original; |
| 153 | + } |
| 154 | + if (notAcceptable != null) { |
| 155 | + throw notAcceptable; |
| 156 | + } |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * When the original URI ends with a registered media-type extension <em>and</em> a trailing |
| 162 | + * slash, returns a request with only the trailing slash removed (extension kept). Otherwise |
| 163 | + * {@code null}. |
| 164 | + */ |
| 165 | + private HttpServletRequest trailingSlashOnly(HttpServletRequest request) { |
| 166 | + String uri = request.getRequestURI(); |
| 167 | + if (uri == null || uri.length() <= 1 || uri.charAt(uri.length() - 1) != PATH_SEPARATOR) { |
| 168 | + return null; |
| 169 | + } |
| 170 | + String withoutSlash = uri.substring(0, uri.length() - 1); |
| 171 | + if (getRegisteredExtension(withoutSlash) == null) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + return new PathNormalizingRequestWrapper(request, null, true); |
128 | 175 | } |
129 | 176 |
|
130 | 177 | private static void clearAndReparsePathCaches(HttpServletRequest request) { |
|
0 commit comments