@Body
示例代码
@Body
注解可用于设置请求体的序列化方式。通过format
参数,可以指定使用的SerializationFormat
,从而生成对应的ContentType
类型的请求体可选的
SerializationFormat
类型及对应的ContentType
如下:SerializationFormat.JSON
对应ContentType.Application.Json
默认值SerializationFormat.XML
对应ContentType.Application.Xml
SerializationFormat.CBOR
对应ContentType.Application.Cbor
SerializationFormat.PROTO_BUF
对应ContentType.Application.ProtoBuf
在这里使用
@Body
注解,这里为format
设置了SerializationFormat.XML
值
@BearerAuth
@POST(url = "user/detail/update")
suspend fun updateUserDetail(
@Body(format = SerializationFormat.XML) data: UserDetail
): Boolean
生成的实现
在实现中这里为
contentType()
函数设置了ContentType.Application.Xml
值,对应注解中设置的SerializationFormat.XML
override suspend fun updateUserDetail(`data`: UserDetail): Boolean {
val token = this.config.token?.invoke()
val response = this.config.httpClient.post {
this.url("user/detail/update")
if (token != null) {
this.bearerAuth(token)
}
this.contentType(ContentType.Application.Xml)
this.setBody(`data`)
}
return response.body()
}
Last modified: 30 July 2025