如何修复Java中解析xml时的JAXB错误?

如何修复Java中解析xml时的JAXB错误?,java,spring-boot,Java,Spring Boot,在字符串变量中有一些xml代码,我希望将其传递给我的JAXB,以便将其解析为java对象。然而,当我运行我的代码时,它似乎没有抓住任何东西。它返回一个带有ClassNotFoundException的错误。我不确定是因为我在类中设置了错误的名称,还是因为我忘记了类中的某种类型的注释。我试图更改一些名称,但仍然收到相同的错误 错误: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on modu

在字符串变量中有一些xml代码,我希望将其传递给我的
JAXB
,以便将其解析为java对象。然而,当我运行我的代码时,它似乎没有抓住任何东西。它返回一个带有
ClassNotFoundException
的错误。我不确定是因为我在类中设置了错误的名称,还是因为我忘记了类中的某种类型的注释。我试图更改一些名称,但仍然收到相同的错误

错误:

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at com.example.taddmsdk.Runner.meToo(Runner.java:18)
    at com.example.taddmsdk.TaddmsdkApplication.run(TaddmsdkApplication.java:75)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.example.taddmsdk.TaddmsdkApplication.main(TaddmsdkApplication.java:31)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
    ... 11 more
<?xml version="1.0" encoding="UTF-8"?>
<results
        xmlns="info"
        xmlns:coll="info"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="info">

    <IIsWebServiceImpl array="1"
        guid="063D0DD72D5F3A46B118D7A963306361" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMWebService">
        <modules array="1" guid="FBVRKJNR75858NND2UJ3J2IC9C9R" xsi:type="coll:com.collation.platform.model.topology.app.web.klm.KLMModule">
            <fileName>D:\pubKSRF</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <modules array="2" guid="IJECI89585U5FJIDNVU3FI2NF" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
            <fileName>d:\SRFdata</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <modules array="3" guid="VNJFVFV8238DNCNCJ3J4JNDIEJC875" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
            <fileName>D:\srfData</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <isPlaceholder>false</isPlaceholder>
        <displayName/>
        <hierarchyDomain>app.web.klm</hierarchyDomain>
        <hierarchyType>KLMWebService</hierarchyType>
    </IIsWebServiceImpl>
</results>
package com.example.xmlClass;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class KLMWebServiceImpl {
     ArrayList <Object> modules = new ArrayList <Object> ();
     private String isPlaceholder;
     private String hierarchyType;
     private String hierarchyDomain;
     private String displayName;
     private String array;
     private String guid;
     private String xsiType;


     //Getters
    public ArrayList<Object> getModules() {
        return modules;
    }
    public String getIsPlaceholder() {
        return isPlaceholder;
    }

    public String getHierarchyType() {
        return hierarchyType;
    }
    public String getHierarchyDomain() {
        return hierarchyDomain;
    }
    public String getDisplayName() {
        return displayName;
    }

    public String getArray() {
        return array;
    }

    public String getGuid() {
        return guid;
    }
    public String getXsiType() {
        return xsiType;
    }





    //Setters
    @XmlElement
    public void setArray(String array) {
        this.array = array;
    }
    @XmlElement
    public void setHierarchyDomain(String hierarchyDomain) {
        this.hierarchyDomain = hierarchyDomain;
    }
    @XmlElement
    public void setGuid(String guid) {
        this.guid = guid;
    }

    @XmlElement
    public void setXsiType(String xsiType) {
        this.xsiType = xsiType;
    }
    @XmlElement
    public void setModules(ArrayList<Object> modules) {
        this.modules = modules;
    }

    @XmlElement
    public void setIsPlaceholder(String isPlaceholder) {
        this.isPlaceholder = isPlaceholder;
    }

    @XmlElement
    public void setHierarchyType(String hierarchyType) {
        this.hierarchyType = hierarchyType;
    }

    @XmlElement
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }



    }
XML:

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at com.example.taddmsdk.Runner.meToo(Runner.java:18)
    at com.example.taddmsdk.TaddmsdkApplication.run(TaddmsdkApplication.java:75)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.example.taddmsdk.TaddmsdkApplication.main(TaddmsdkApplication.java:31)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
    ... 11 more
<?xml version="1.0" encoding="UTF-8"?>
<results
        xmlns="info"
        xmlns:coll="info"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="info">

    <IIsWebServiceImpl array="1"
        guid="063D0DD72D5F3A46B118D7A963306361" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMWebService">
        <modules array="1" guid="FBVRKJNR75858NND2UJ3J2IC9C9R" xsi:type="coll:com.collation.platform.model.topology.app.web.klm.KLMModule">
            <fileName>D:\pubKSRF</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <modules array="2" guid="IJECI89585U5FJIDNVU3FI2NF" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
            <fileName>d:\SRFdata</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <modules array="3" guid="VNJFVFV8238DNCNCJ3J4JNDIEJC875" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
            <fileName>D:\srfData</fileName>
            <isPlaceholder>false</isPlaceholder>
            <displayName/>
            <hierarchyDomain>app.web.klm</hierarchyDomain>
            <hierarchyType>KLMModule</hierarchyType>
        </modules>
        <isPlaceholder>false</isPlaceholder>
        <displayName/>
        <hierarchyDomain>app.web.klm</hierarchyDomain>
        <hierarchyType>KLMWebService</hierarchyType>
    </IIsWebServiceImpl>
</results>
package com.example.xmlClass;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class KLMWebServiceImpl {
     ArrayList <Object> modules = new ArrayList <Object> ();
     private String isPlaceholder;
     private String hierarchyType;
     private String hierarchyDomain;
     private String displayName;
     private String array;
     private String guid;
     private String xsiType;


     //Getters
    public ArrayList<Object> getModules() {
        return modules;
    }
    public String getIsPlaceholder() {
        return isPlaceholder;
    }

    public String getHierarchyType() {
        return hierarchyType;
    }
    public String getHierarchyDomain() {
        return hierarchyDomain;
    }
    public String getDisplayName() {
        return displayName;
    }

    public String getArray() {
        return array;
    }

    public String getGuid() {
        return guid;
    }
    public String getXsiType() {
        return xsiType;
    }





    //Setters
    @XmlElement
    public void setArray(String array) {
        this.array = array;
    }
    @XmlElement
    public void setHierarchyDomain(String hierarchyDomain) {
        this.hierarchyDomain = hierarchyDomain;
    }
    @XmlElement
    public void setGuid(String guid) {
        this.guid = guid;
    }

    @XmlElement
    public void setXsiType(String xsiType) {
        this.xsiType = xsiType;
    }
    @XmlElement
    public void setModules(ArrayList<Object> modules) {
        this.modules = modules;
    }

    @XmlElement
    public void setIsPlaceholder(String isPlaceholder) {
        this.isPlaceholder = isPlaceholder;
    }

    @XmlElement
    public void setHierarchyType(String hierarchyType) {
        this.hierarchyType = hierarchyType;
    }

    @XmlElement
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }



    }

你用的是Java 9+?@dan1st我用的是Java 8