XML无法在marshal上创建JAXBContext [英] XML unable to create JAXBContext on marshal
问题描述
我有一个POJO(在Kotlin中),我想改为XML,但我遇到问题需要通过 JAXBContext.newInstance(myObj :: class.java)
部分
I have a POJO(in Kotlin) that I wanted to change into XML but i having problem going through the JAXBContext.newInstance(myObj::class.java)
part
只需在Java / Kotlin中查看/回复
这是我的编组上的代码
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 ON)
Here's my code on the POJO or data class (I tried both and neither with/without @XmlType & @XmlElement ON)
@XmlRootElement(name = "WxPayOrder")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = arrayOf("appid","attach","body","detail"))
data class WxPayOrder (
@XmlElement(name = "appid")
var appid: String,
@XmlElement(name = "attach")
var attach: String? = null,
@XmlElement(name = "body")
var body: String,
@XmlElement(name = "detail")
var detail: String? = null,
)
这是我得到的错误(我个人认为)那个信息不够,我看到其他人遇到这个错误也有重复的名字等......但不是我的。)
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:471)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:303)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1156)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
这是我的pom.xml 的一部分(我只是包含它的一部分因为我害怕我遗漏了一些重要的东西,你们可能会看到它)
Here's my part of my pom.xml (i just include part of it because i scare i left out something important that you guys might able to see it)
<!-- 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>
我对Kotlin和JAXB很新。提前致谢。
I'm quite new to Kotlin and JAXB. Thanks in advance.
推荐答案
JAXB需要一个无参数构造函数才能工作。也许你的stacktrace / log中的某个地方也写了......但也许没有。
JAXB requires a no-arg-constructor to work. Maybe somewhere in your stacktrace/log that was also written... but maybe not.
当你使用数据类时,解决问题的最简单方法是添加一个 no-arg-constructor
如下:
As you used a data class, the simplest way to fix the issue is to add a no-arg-constructor
as follows:
@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 */)
}
如果是简单类
,你也可以使用类似的东西:
If it was a "simple" class
you can also use something similar to:
@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
}
}
这篇关于XML无法在marshal上创建JAXBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!