Skip to content

Commit e99791f

Browse files
zamzterzrstoyanchev
authored andcommitted
Improve i18n-support for NoResourceFoundException.
Return the requested resource as ErrorResponse.getDetailMessageArguments, making it usable with message customization and i18n. See gh-35758 Signed-off-by: Samuel Gulliksson <[email protected]>
1 parent 658775b commit e99791f

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/mvc-ann-rest-exceptions.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Message codes and arguments for each error are also resolved via `MessageSource`
171171

172172
| `NoResourceFoundException`
173173
| (default)
174-
|
174+
| `+{0}+` the resource
175175

176176
| `TypeMismatchException`
177177
| (default)

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/NoResourceFoundException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ public ProblemDetail getBody() {
7878
return this.body;
7979
}
8080

81+
@Override
82+
public Object[] getDetailMessageArguments() {
83+
return new String[]{this.resourcePath};
84+
}
8185
}

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/NoResourceFoundExceptionTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ void detailShouldContainResourcePath() {
4040
assertThat(noResourceFoundException.getBody().getDetail()).isEqualTo("No static resource /resource.");
4141
}
4242

43+
@Test
44+
void messageArgumentsShouldContainResourcePath() {
45+
var noResourceFoundException = new NoResourceFoundException(HttpMethod.GET, "/context/resource", "/resource");
46+
assertThat(noResourceFoundException.getDetailMessageArguments()).containsExactly("/resource");
47+
}
4348
}

0 commit comments

Comments
 (0)