Garzec
Garzec
Explore posts from servers
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
yes, I think I got this ( as shown in my code above )
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
no, same for INTERNAL_SERVER_ERROR Is there some Spring magic required? Do I have to place this exception handler file into a specific directory with a conventional name or so...
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
yes, the default Spring behavior for invalid request bodies I guess
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
if /foo is valid, it executes the rest handler, if it's invalid then Spring responds with a 400, so handleValidationException never runs
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
sorry, the function handleValidationException never runs
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
yes please
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
you only get a 400 with
{
"timestamp": "2025-01-12T18:11:01.102+00:00",
"path": "/foo",
"status": 400,
"error": "Bad Request",
"requestId": "2e927c69-2"
}
{
"timestamp": "2025-01-12T18:11:01.102+00:00",
"path": "/foo",
"status": 400,
"error": "Bad Request",
"requestId": "2e927c69-2"
}
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
nothing, when I set a breakpoint, it won't run the function
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
you mean like so?
import org.springframework.http.HttpStatus
import org.springframework.validation.FieldError
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class ValidationExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidationException(
exception: MethodArgumentNotValidException
): ValidationErrorResponse {
val fieldErrors = exception.bindingResult.allErrors.associate { error ->
val fieldName = (error as FieldError).field
val errorMessage = error.defaultMessage

fieldName to errorMessage
}

return ValidationErrorResponse(fieldErrors)
}
}

data class ValidationErrorResponse(val fieldErrors: Map<String, String?>)
import org.springframework.http.HttpStatus
import org.springframework.validation.FieldError
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class ValidationExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidationException(
exception: MethodArgumentNotValidException
): ValidationErrorResponse {
val fieldErrors = exception.bindingResult.allErrors.associate { error ->
val fieldName = (error as FieldError).field
val errorMessage = error.defaultMessage

fieldName to errorMessage
}

return ValidationErrorResponse(fieldErrors)
}
}

data class ValidationErrorResponse(val fieldErrors: Map<String, String?>)
That still doesn't work 😦
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
@ayylmao123xdd thank you. I tried to follow the blog post and just changed from @ControllerAdvice to @RestControllerAdvice since ErrorMessage is just a custom type, I want to make use of problem details later on Still, doesn't work 😦
56 replies
JCHJava Community | Help. Code. Learn.
Created by Garzec on 1/12/2025 in #java-help
How to apply request body validation with error details?
would you mind telling me how to do this? ValidationExceptionHandler is wrong?
56 replies