Ktor 客户端 XML:将对象属性序列化为 XML 个子项
Ktor Client XML: Serialize Object Properties as XML Children
是否可以设置 Ktor 客户端将对象属性序列化为 XML 个子项?
<PersonDTOXML>
<id>1</id>
<name>John</name>
<age>20</age>
</PersonDTOXML>
默认情况下,对象属性被序列化为 XML 个属性。
<PersonDTOXML id="1" name="John" age="20" />
我正在学习 https://ktor.io/docs/serialization-client.html#receive_send_data
上的教程
//==================================================================
// MAIN ACTIVITY
//==================================================================
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var person by remember { mutableStateOf(PersonDTOXML(0, "No name", 0)) }
val coroutineScope = rememberCoroutineScope()
Button(onClick = { coroutineScope.launch { person = callURL() } }) {
Text("RESPONSE: $person")
}
}
}
}
//==================================================================
// CALL URL
//==================================================================
suspend fun callURL() : PersonDTOXML {
//CONFIGURE CLIENT
val client = HttpClient(CIO) {
install(ContentNegotiation){ xml() }
}
//CAL URL
val person: PersonDTOXML = client.get("http://192.168.0.102:8080/ReceiveBodyXML").body()
//CLOSE CLIENT
client.close()
//RETURN PERSON
println(person) //Person(id=1, name=John, age=20)
return person;
}
//==================================================================
// PERSONDTOXML
//==================================================================
@Serializable
data class PersonDTOXML(val id: Int, val name: String, val age: Int)
您可以使用 XmlElement
注释来注释 PersonDTOXML
class 的属性,以强制将它们序列化为 XML 元素:
@kotlinx.serialization.Serializable
data class PersonDTOXML(@XmlElement(true) val id: Int, @XmlElement(true) val name: String, @XmlElement(true) val age: Int)
另外,你可以写一个custom policy来重新定义Kotlin结构如何翻译成XML。
是否可以设置 Ktor 客户端将对象属性序列化为 XML 个子项?
<PersonDTOXML>
<id>1</id>
<name>John</name>
<age>20</age>
</PersonDTOXML>
默认情况下,对象属性被序列化为 XML 个属性。
<PersonDTOXML id="1" name="John" age="20" />
我正在学习 https://ktor.io/docs/serialization-client.html#receive_send_data
上的教程//==================================================================
// MAIN ACTIVITY
//==================================================================
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var person by remember { mutableStateOf(PersonDTOXML(0, "No name", 0)) }
val coroutineScope = rememberCoroutineScope()
Button(onClick = { coroutineScope.launch { person = callURL() } }) {
Text("RESPONSE: $person")
}
}
}
}
//==================================================================
// CALL URL
//==================================================================
suspend fun callURL() : PersonDTOXML {
//CONFIGURE CLIENT
val client = HttpClient(CIO) {
install(ContentNegotiation){ xml() }
}
//CAL URL
val person: PersonDTOXML = client.get("http://192.168.0.102:8080/ReceiveBodyXML").body()
//CLOSE CLIENT
client.close()
//RETURN PERSON
println(person) //Person(id=1, name=John, age=20)
return person;
}
//==================================================================
// PERSONDTOXML
//==================================================================
@Serializable
data class PersonDTOXML(val id: Int, val name: String, val age: Int)
您可以使用 XmlElement
注释来注释 PersonDTOXML
class 的属性,以强制将它们序列化为 XML 元素:
@kotlinx.serialization.Serializable
data class PersonDTOXML(@XmlElement(true) val id: Int, @XmlElement(true) val name: String, @XmlElement(true) val age: Int)
另外,你可以写一个custom policy来重新定义Kotlin结构如何翻译成XML。