I need to receive 4 different request bodies in th...
# spring
j
I need to receive 4 different request bodies in the same endpoint, is there a simple way to do it?
f
This is not really Spring specific but you could use a sealed class and JSON subtypes
Copy code
@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes(
    Type(A::class),
    Type(B::class),
)
sealed class MyRequest {
   data class A(val someInt: Int) : MyRequest()
   data class B(val someString: String, val someOtherType: Float) : MyRequest()
}
Copy code
@PostMapping
fun someControllerMethod(@RequestBody myRequest: MyRequest) = when(myRequest) {
}
馃憤 1
j
thanks!
it鈥檚 not really working because the bodies are too similar. Only a couple fields are dropped or added from one class to the other so jackson seems to get confused.
f
there are options other than
DEDUCTION
that will let you use a selector to determine the type
e
U have to map the little changes in your DTOs and initialize on nulls the other name of values
Using ? Operator
Or validate the data before to use