英文:
how to use kotlin data class and springboot-Validation to verify Null type at the same time?
问题
data class GetSourceListRequest(
@NotNull(message = "category not allow null")
val category: List<Int>
)
// Then use `@ControllerAdvice` to handle exceptions.
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
fun <T> httpMessageNotReadableExceptionHandler(e: HttpMessageNotReadableException): String {
return e.message
}
// The error message obtained is as follows:
// > JSON parse error: Instantiation of [simple type, class com.sage.feature.source.database.request.GetSourceListRequest] value failed for JSON property category due to missing (therefore NULL) value for creator parameter category which is a non-nullable type
//
// But it's not what I expected, `@NotNull` didn't work because the json conversion had an error before that.
//
// I found a related [question](https://stackoverflow.com/questions/49896933/kotlin-data-class-and-bean-validation-notnull-on-long-fields-does-not-work), but the solution it gives is to make the property a nullable type, which is not the perfect solution.
//
// How to use springboot-validation in this case?
英文:
data class GetSourceListRequest(
@NotNull(message = "category not allow null")
val category: List<Int>
)
Then use @ControllerAdvice
to handle exceptions.
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
fun <T> httpMessageNotReadableExceptionHandler(e: HttpMessageNotReadableException): String {
return e.message
}
The error message obtained is as follows:
> JSON parse error: Instantiation of [simple type, class com.sage.feature.source.database.request.GetSourceListRequest] value failed for JSON property category due to missing (therefore NULL) value for creator parameter category which is a non-nullable type
But it's not what I expected, @NotNull
didn't work because the json conversion had an error before that.
I found a related question, but the solution it gives is to make the property a nullable type, which is not the perfect solution.
How to use springboot-validation in this case?
答案1
得分: 1
Have you tried making the category field nullable?:
data class GetSourceListRequest(
@NotNull(message = "category not allow null")
val category: List<Int>?
)
I know this functionally changes your Request object but this is the general problem I have found when you want to use later-firing validation - the "carrier" object needs to allow for nulls.
The problem goes further with enum
values where you cannot necessarily get a good error message if the type only permits valid enum values.
An alternate suggestion for you is to consider a Kotlin-native validation framework that works alongside Spring: Konform (we made our own annotation to invoke the Konform validation automatically on Controllers where we wanted this validation framework).
Even using Konform, we end up with the same thing - request objects whose fields are all nullable and then either using accessor methods with the !!
syntax because we know by the same the object is accessed the validation has occurred, or transforming to a strongly typed object.
英文:
Have you tried making the category field nullable?:
data class GetSourceListRequest(
@NotNull(message = "category not allow null")
val category: List<Int>?
)
I know this functionally changes your Request object but this is the general problem I have found when you want to use later-firing validation - the "carrier" object needs to allow for nulls.
The problem goes further with enum
values where you cannot necessarily get a good error message if the type only permits valid enum values.
An alternate suggestion for you is to consider a Kotlin-native validation framework that works alongside Spring : Konform (we made our own annotation to invoke the Konform validation automatically on Controllers where we wanted this validation framework).
Even using Konform, we end up with the same thing - request objects whose fields are all nullable and then either using accessor methods with the !!
syntax because we know by the same the object is accessed the validation has occurred, or transforming to a strongly typed object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论