gpt4 book ai didi

java - XML 无法在编码上创建 JAXBContext

转载 作者:行者123 更新时间:2023-12-02 10:53:14 27 4
gpt4 key购买 nike

我有一个 POJO(Kotlin 中),我想将其更改为 XML,但我在查看 JAXBContext.newInstance(myObj::class.java) 时遇到问题部分

只需在 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)

@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 的一部分(我只包含其中的一部分,因为我担心我遗漏了一些你们可能会看到的重要内容)

<!-- 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 还很陌生。提前致谢。

最佳答案

JAXB 需要无参数构造函数才能工作。也许你的堆栈跟踪/日志中的某个地方也被写入......但也许没有。

由于您使用了数据类,解决该问题的最简单方法是添加 no-arg-constructor,如下所示:

@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
}
}

关于java - XML 无法在编码上创建 JAXBContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com