generator-jhipster
generator-jhipster copied to clipboard
Error HandlerMethodValidationException not handle
Overview of the issue
Error HandlerMethodValidationException didn't handle when validation at request param
Motivation for or Use Case
Create a REST API with valid data at @RequestParam
Reproduce the error
When I create an API and valid data of jakarta.validation package
@PutMapping("/sell-abcxyz")
@PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.USER + "\")")
public ResponseEntity<AbcResponse> sellPlant(@RequestParam Long userId,
@RequestParam @Min(value = 1, message = "Must be greater than zero") Integer quantity) {
AbcResponse userItem = userItemService.sellItem(userId, quantity);
return ResponseEntity.ok(userItem);
}
The actual response not showing message of error:
{
"type": "about:blank/problem-with-message",
"title": "Bad Request",
"status": 400,
"detail": "400 BAD_REQUEST \"Validation failure\"",
"instance": "/api/user-items/sell-abcxyz",
"message": "error.validation",
"path": "/api/user-items/sell-abcxyz"
}
I need a specific error message. At least, the error message must show the default value of jakarta validation error message
Related issues
Suggest a Fix
I think, in the ExceptionTranslator.getProblemDetailWithCause method you need to handle HandlerMethodValidationException exception like:
if (ex instanceof HandlerMethodValidationException methodValidationException) {
return ProblemDetailWithCauseBuilder.instance()
.withStatus(HttpStatus.BAD_REQUEST.value())
.withType(ErrorConstants.DEFAULT_TYPE)
.withTitle(methodValidationException.getReason())
.withDetail(methodValidationException.getValueResults().get(0)
.getResolvableErrors().get(0).getDefaultMessage())
.build();
}
then the response looks like this:
{
"type": "about:blank/problem-with-message",
"title": "Bad Request",
"status": 400,
"detail": "Quantity must be greater than zero",
"instance": "/api/user-items/sell-abcxyz",
"message": "error.http.400",
"path": "/api/user-items/sell-abcxyz"
}
JHipster Version(s)
8.7.1
Browsers and Operating System
MacOS 13.5, JDK 17
- [x] Checking this box is mandatory (this is just to show you read everything)