XML无法在元帅上创建JAXBContext

时间:2018-08-24 03:31:01

标签: java xml kotlin jaxb marshalling

我有一个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很陌生。提前致谢。

1 个答案:

答案 0 :(得分:1)

JAXB需要一个无参数的构造函数才能工作。也许在您的stacktrace / log中也已经写过的某处……但是也许没有。

使用数据类时,解决此问题的最简单方法是添加On Error Resume Next Sheets(1).Select Worksheets.Add Sheets(1).Name = "Consolidated" Sheets(2).Activate Range("A1").EntireRow.Select Selection.Copy Destination:=Sheets(1).Range("A1") For Each s In ActiveWorkbook.Sheets If s.Name <> "Consolidated" And s.Name <> "Report" And s.Name <> "Table" And s.Name <> "Values" And s.Name <> "Button" And s.Name <> "PivotTable" And s.Name <> "Task" And s.Name <> "Mapping" Then Application.GoTo Sheets(s.Name).[a1] Selection.CurrentRegion.Select Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select Selection.Copy Destination:=Sheets("Consolidated"). _ Cells(Rows.Count, 1).End(xlUp)(3) _ .PasteSpecial(xlPasteValues).PasteSpecial(xlPasteFormats) '<<-- The problem. It does not give me error but it only give me a blank cell everytime I add that line of code. End If Next ,如下所示:

public static boolean isAdmin() {
    try {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe");
        Process process = processBuilder.start();
        PrintStream printStream = new PrintStream(process.getOutputStream(), true);
        Scanner scanner = new Scanner(process.getInputStream());
        printStream.println("@echo off");
        printStream.println(">nul 2>&1 \"%SYSTEMROOT%\\system32\\cacls.exe\" \"%SYSTEMROOT%\\system32\\config\\system\"");
        printStream.println("echo %errorlevel%");

        boolean printedErrorlevel = false;
        while (true) {
            String nextLine = scanner.nextLine();
            if (printedErrorlevel) {
                int errorlevel = Integer.parseInt(nextLine);
                return errorlevel == 0;
            } else if (nextLine.equals("echo %errorlevel%")) {
                printedErrorlevel = true;
            }
        }
    } catch (IOException e) {
        return false;
    }
}

如果它是“简单” 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 */)
}