diff --git a/spring-context/src/main/java/org/springframework/validation/FieldError.java b/spring-context/src/main/java/org/springframework/validation/FieldError.java index efef6971ab3..a92e49710ab 100644 --- a/spring-context/src/main/java/org/springframework/validation/FieldError.java +++ b/spring-context/src/main/java/org/springframework/validation/FieldError.java @@ -16,8 +16,6 @@ package org.springframework.validation; -import java.util.HexFormat; - import org.jspecify.annotations.Nullable; import org.springframework.util.Assert; @@ -127,18 +125,8 @@ public class FieldError extends ObjectError { // We would preferably use ObjectUtils.nullSafeConciseToString(rejectedValue) here but // keep including the full nullSafeToString representation for backwards compatibility. return "Field error in object '" + getObjectName() + "' on field '" + this.field + - "': rejected value [" + formatRejectedValue() + "]; " + + "': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " + resolvableToString(); } - private String formatRejectedValue() { - - // Special handling of byte[], to be moved into ObjectUtils in 7.0 - if (this.rejectedValue instanceof byte[] bytes && bytes.length != 0) { - return "{" + HexFormat.of().formatHex(bytes) + "}"; - } - - return ObjectUtils.nullSafeToString(this.rejectedValue); - } - } diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index c32075563ea..a4db2ad8553 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java @@ -21,6 +21,7 @@ import java.nio.charset.Charset; import java.time.ZoneId; import java.util.Arrays; import java.util.Collection; +import java.util.HexFormat; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -695,9 +696,9 @@ public abstract class ObjectUtils { /** * Return a String representation of the contents of the specified array. - *

The String representation consists of a list of the array's elements, - * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated - * by the characters {@code ", "} (a comma followed by a space). + *

As of 7.0, the String representation is a hex-encoded string enclosed + * in curly braces ({@code "{}"}). The String consists of 2 hexadecimal + * chars per element, and without a delimiter between adjacent elements. * Returns a {@code "null"} String if {@code array} is {@code null}. * @param array the array to build a String representation for * @return a String representation of {@code array} @@ -709,11 +710,7 @@ public abstract class ObjectUtils { if (array.length == 0) { return EMPTY_ARRAY; } - StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END); - for (byte b : array) { - stringJoiner.add(String.valueOf(b)); - } - return stringJoiner.toString(); + return ARRAY_START + HexFormat.of().formatHex(array) + ARRAY_END; } /** diff --git a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java index f370e48dcb8..cda0bbe610b 100644 --- a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java @@ -630,8 +630,8 @@ class ObjectUtilsTests { @Test void nullSafeToStringWithByteArray() { - byte[] array = {5, 8}; - assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{5, 8}"); + byte[] array = {5, 38}; + assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{0526}"); } @Test