Skip to content

MethodArgumentNotValidException::errorsToStringList should include only the resolved message when MessageSource is used #30198

Closed
@9450035

Description

@9450035

Affects: 6.0.4

I try to use error handling by setting problemdetails.enabled=true and internationalising error messages. But I can't achieve my expected result Because in org.springframework.web.bind.MethodArgumentNotValidException::errorsToStringList field and single quotations were added to messages.

private static List<String> errorsToStringList(
List<? extends ObjectError> errors, Function<ObjectError, String> formatter) {

		List<String> result = new ArrayList<>(errors.size());
		for (ObjectError error : errors) {
			String value = formatter.apply(error);
			if (StringUtils.hasText(value)) {
				result.add(error instanceof FieldError fieldError ?
						fieldError.getField() + ": '" + value + "'" : "'" + value + "'");
			}
		}
		return result;
	}

Is there any way to internationalize the field name or remove the field name and brackets from messages?

messages.properties

problemDetail.org.springframework.web.bind.support.WebExchangeBindException= Format is incorrect because {1}
Pattern.phoneNumber= phone number is not correct
Pattern.email= email is not correct

Here is sample output:

{
  "type": "about:blank",
  "title": "problemDetail.title.org.springframework.web.bind.support.WebExchangeBindException",
  "status": 400,
  "detail": "Format is incorrect because [phoneNumber: 'phone number is not correct',email: 'email is not correct']",
  "instance": "/profile"
}

But I expect:

{
  "type": "about:blank",
  "title": "problemDetail.title.org.springframework.web.bind.support.WebExchangeBindException",
  "status": 400,
  "detail": "Format is incorrect because phone number is not correct and email is not correct",
  "instance": "/profile"
}

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions