Skip to content

Enhance handleTypeMismatch error message in ResponseEntityExceptionHandler #35837

@MelleD

Description

@MelleD

Currently, the handleTypeMismatch method in ResponseEntityExceptionHandler does not include information about the required type in its error message.

I’d like to extend this handler so that the response includes the expected type information when a TypeMismatchException occurs. This would make debugging and client error handling easier, as users could immediately see which type was expected.

https://github.com/spring-projects/spring-framework/blob/main/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java#L484

e.g.

String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'";
final Class<?> requiredType = e.getRequiredType();
if ( requiredType != null ) {
   String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "' to be a valid " + requiredType.getSimpleName(); 
}

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions