diff --git a/spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java b/spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java index ae2725b632c..57cedd589d1 100644 --- a/spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java @@ -50,6 +50,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { private String titleMessageCode; + private String typeMessageCode; + DefaultErrorResponseBuilder(Throwable ex, HttpStatusCode statusCode, String detail) { Assert.notNull(ex, "Throwable is required"); @@ -60,6 +62,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { this.problemDetail = ProblemDetail.forStatusAndDetail(statusCode, detail); this.detailMessageCode = ErrorResponse.getDefaultDetailMessageCode(ex.getClass(), null); this.titleMessageCode = ErrorResponse.getDefaultTitleMessageCode(ex.getClass()); + this.typeMessageCode = ErrorResponse.getDefaultTypeMessageCode(ex.getClass()); } @@ -102,6 +105,12 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { return this; } + @Override + public ErrorResponse.Builder typeMessageCode(String messageCode) { + this.typeMessageCode = messageCode; + return this; + } + @Override public ErrorResponse.Builder title(@Nullable String title) { this.problemDetail.setTitle(title); @@ -131,7 +140,8 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { public ErrorResponse build() { return new SimpleErrorResponse( this.exception, this.statusCode, this.headers, this.problemDetail, - this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode); + this.detailMessageCode, this.detailMessageArguments, this.titleMessageCode, + this.typeMessageCode); } @@ -155,9 +165,12 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { private final String titleMessageCode; + private final String typeMessageCode; + SimpleErrorResponse( Throwable ex, HttpStatusCode statusCode, @Nullable HttpHeaders headers, ProblemDetail problemDetail, - String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode) { + String detailMessageCode, @Nullable Object[] detailMessageArguments, String titleMessageCode, + String typeMessageCode) { this.exception = ex; this.statusCode = statusCode; @@ -166,6 +179,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { this.detailMessageCode = detailMessageCode; this.detailMessageArguments = detailMessageArguments; this.titleMessageCode = titleMessageCode; + this.typeMessageCode = typeMessageCode; } @Override @@ -198,6 +212,11 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { return this.titleMessageCode; } + @Override + public String getTypeMessageCode() { + return this.typeMessageCode; + } + @Override public String toString() { return "ErrorResponse{status=" + this.statusCode + ", " + diff --git a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java index 27123f1bb19..39f7ee06ba8 100644 --- a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java +++ b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java @@ -239,6 +239,17 @@ public interface ErrorResponse { */ Builder type(URI type); + /** + * Customize the {@link MessageSource} code to use to resolve the value + * for {@link ProblemDetail#setType(URI)}. + *

By default, set from {@link ErrorResponse#getDefaultTypeMessageCode(Class)}. + * @param messageCode the message code to use + * @return the same builder instance + * @since 6.1 + * @see ErrorResponse#getTypeMessageCode() + */ + Builder typeMessageCode(String messageCode); + /** * Set the underlying {@link ProblemDetail#setTitle(String) title} field. * @return the same builder instance @@ -246,11 +257,9 @@ public interface ErrorResponse { Builder title(@Nullable String title); /** - * Customize the {@link MessageSource} code for looking up the value for - * the underlying {@link ProblemDetail#setTitle(String) title}. - *

By default, set via - * {@link ErrorResponse#getDefaultTitleMessageCode(Class)} with the - * associated Exception type. + * Customize the {@link MessageSource} code to use to resolve the value + * for {@link ProblemDetail#setTitle(String)}. + *

By default, set from {@link ErrorResponse#getDefaultTitleMessageCode(Class)}. * @param messageCode the message code to use * @return the same builder instance * @see ErrorResponse#getTitleMessageCode() @@ -270,11 +279,9 @@ public interface ErrorResponse { Builder detail(String detail); /** - * Customize the {@link MessageSource} code for looking up the value for - * the underlying {@link #detail(String) detail}. - *

By default, this is set to - * {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)} with the - * associated Exception type. + * Customize the {@link MessageSource} code to use to resolve the value + * for the {@link #detail(String)}. + *

By default, set from {@link ErrorResponse#getDefaultDetailMessageCode(Class, String)}. * @param messageCode the message code to use * @return the same builder instance * @see ErrorResponse#getDetailMessageCode()