我试图在Feign中使用一个接口作为RequestBody,但Feign正在创建一个空对象作为请求。这是不可能的,还是我在这里做错了什么?到目前为止,我找不到任何关于那个话题的东西。
这是我尝试做什么的一个简化示例(实际上有3种不同类型的请求(
interface BookingClient {
@RequestLine("POST /booking")
@Headers("Content-Type: application/json")
fun createBooking(request: BookingRequest): BookingResponse
}
interface BookingRequest
data class NormalBooking(
val product: String
): BookingRequest
data class DiscountedBooking(
val product: String,
val discountCode: String
): BookingRequest
// Client Configuration
val client = Feign.builder()
.client(feign.okhttp.OkHttpClient())
.errorDecoder(BadEntityErrorDecoder())
.encoder(JacksonEncoder())
.decoder(JacksonDecoder(listOf(KotlinModule(), JavaTimeModule())))
.logger(feign.Logger.JavaLogger())
.logLevel(feign.Logger.Level.FULL)
.target(BookingClient::class.java, mockServer.getUrl())
如果我现在用任一实现调用createBooking()
,Feign总是序列化
{}
而不是
{
"product": "productA"
}
和
{
"product": "productA",
"discountCode": "discountCode"
}
问题是参数类型,当我们调用Feign.target(…(时,Feign开始将您的BookingClient解析为Http请求模板,而主体类型是BookingRequest,因此Feign总是序列化"{}">
interface BookingClient {
@RequestLine("POST /booking")
@Headers("Content-Type: application/json")
// BookingRequest should change to NormalBooking or DiscountedBooking
fun createBooking(request: BookingRequest): BookingResponse
}