XML unable to create JAXBContext on marshal [duplicate]

前提是你 提交于 2019-12-24 18:28:33

问题


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

Just view/reply it in Java/Kotlin also can

Here's my code on marshalling

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()

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,
)

Here's is the error i getting (I personally think that wasn't informative enough and i saw others that hit this error also come with duplicate name etc.. but not mine)

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)

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>

I'm quite new to Kotlin and JAXB. Thanks in advance.


回答1:


JAXB requires a no-arg-constructor to work. Maybe somewhere in your stacktrace/log that was also written... but maybe not.

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


来源:https://stackoverflow.com/questions/51997055/xml-unable-to-create-jaxbcontext-on-marshal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!