Java XML无法在封送处理上创建JAXBContext
我有一个POJOin Kotlin,我想将其转换为XML,但在处理JAXBContext.newInstancemyObj::class.java部分时遇到了问题 只需在Java/Kotlin中查看/回复即可 这是我的编组代码Java XML无法在封送处理上创建JAXBContext,java,xml,kotlin,jaxb,marshalling,Java,Xml,Kotlin,Jaxb,Marshalling,我有一个POJOin Kotlin,我想将其转换为XML,但在处理JAXBContext.newInstancemyObj::class.java部分时遇到了问题 只需在Java/Kotlin中查看/回复即可 这是我的编组代码 val context = JAXBContext.newInstance(WxPayOrder::class.java) val m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTE
val context = JAXBContext.newInstance(WxPayOrder::class.java)
val m = context.createMarshaller()
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
val sw = StringWriter()
m.marshal(wxPayOrderWithSign, sw)
val xmlString = sw.toString()
这是我在POJO或数据类上的代码,我尝试了两者,但没有使用/没有使用
@上的XmlType和@XmlElement
这里是我得到的错误,我个人认为这是不够的信息,我看到其他击中这个错误也有重复的名称等。。但不是我的
这是我的pom.xml文件的一部分,我只包含了其中的一部分,因为我担心我遗漏了一些重要的东西,你们可能会看到
我对Kotlin和JAXB很陌生。提前谢谢 JAXB需要无参数构造函数才能工作。也许在你的stacktrace/日志中的某个地方也写了。。。但也许不是 使用数据类时,解决此问题的最简单方法是添加一个无参数构造函数,如下所示:
@XmlRootElement
data class WxPayOrder (
// all the properties
) {
// the no-arg-constructor is a must:
constructor() : this("", body = "", /* all the other properties that must have a value, setting them to a default one */)
}
如果它是一个简单的类,您也可以使用类似于:
@XmlRootElement
class WxPayOrder() { // actually now this line contains the no-arg constructor
// your properties
lateinit var demo : String
// your custom constructors
constructor(demo : String) : this() {
this.demo = demo
}
}
你已经问过类似的问题了。。。问题的实际原因是相同的。请同时查看您过去提出的问题的答案。有时,如果你投票给其他人或将其标记为已解决(如果答案有用),他们可能会发现这很有用。实际上,在这种情况下,它是解决您当前问题的方法-
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
@XmlRootElement
data class WxPayOrder (
// all the properties
) {
// the no-arg-constructor is a must:
constructor() : this("", body = "", /* all the other properties that must have a value, setting them to a default one */)
}
@XmlRootElement
class WxPayOrder() { // actually now this line contains the no-arg constructor
// your properties
lateinit var demo : String
// your custom constructors
constructor(demo : String) : this() {
this.demo = demo
}
}