在动态jaxb中获取异常

时间:2018-03-22 08:34:06

标签: java xml soap xsd eclipselink

我遇到异常,我需要从xsd创建动态xml,我正在关注eclipse链接documentation

Exception in thread "main" java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider org.eclipse.persistence.jaxb.plugins.BeanValidationPlugin could not be instantiated
    at java.util.ServiceLoader.fail(ServiceLoader.java:232)
    at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at com.sun.tools.xjc.Options.findServices(Options.java:910)
    at com.sun.tools.xjc.Options.getAllPlugins(Options.java:351)
    at com.sun.tools.xjc.reader.AbstractExtensionBindingChecker.<init>(AbstractExtensionBindingChecker.java:94)
    at com.sun.tools.xjc.reader.ExtensionBindingChecker.<init>(ExtensionBindingChecker.java:77)
    at com.sun.tools.xjc.ModelLoader$XMLSchemaParser.parse(ModelLoader.java:257)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:337)
    at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:124)
    at com.sun.xml.xsom.impl.parser.ParserContext.<init>(ParserContext.java:96)
    at com.sun.xml.xsom.parser.XSOMParser.<init>(XSOMParser.java:125)
    at com.sun.tools.xjc.ModelLoader.createXSOMParser(ModelLoader.java:420)
    at com.sun.tools.xjc.ModelLoader.createXSOMParser(ModelLoader.java:428)
    at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:509)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:236)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:85)
    at org.eclipse.persistence.jaxb.dynamic.metadata.SchemaMetadata.getJavaModelInput(SchemaMetadata.java:155)
    at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext$SchemaContextInput.createContextState(DynamicJAXBContext.java:349)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:196)
    at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext.<init>(DynamicJAXBContext.java:84)
    at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromXSD(DynamicJAXBContextFactory.java:279)
    at com.accenture.App.main(App.java:21)
Caused by: java.lang.NoClassDefFoundError: com/sun/tools/xjc/model/CPropertyVisitor2
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: com.sun.tools.xjc.model.CPropertyVisitor2
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 27 more

我的主类代码是:

package com.accenture;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.internal.oxm.Marshaller;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBMarshaller;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;

public class App {
    public static void main(String[] args) throws JAXBException, FileNotFoundException {
        System.out.println("Hello World!");
        System.setProperty("com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.noCorrectnessCheck", "true");

        FileInputStream xsdInputStream = new FileInputStream("src/main/java/com/accenture/customer.xsd");
        DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream,
                new MyEntityResolver(), null, null);

        DynamicEntity customer = jaxbContext.newDynamicEntity("org.example.Customer");
        customer.set("name", "Jane Doe");

        DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
        address.set("street", "1 Any Street").set("city", "Any Town");
        customer.set("address", address);

        JAXBMarshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(customer, System.out);

    }
}

实体解析器代码是:

package com.accenture;

import java.io.File;
import java.io.IOException;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

class MyEntityResolver implements EntityResolver {

    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        // Imported schemas are located in ext\appdata\xsd\

        // Grab only the filename part from the full path
        String filename = new File(systemId).getName();

        // Now prepend the correct path
        String correctedId = "src/main/java/com/accenture/" + filename;

        InputSource is = new InputSource(ClassLoader.getSystemResourceAsStream(correctedId));
        is.setSystemId(correctedId);

        return is;
    }

}

我的目标是通过引用XSD动态创建XML。我找到的最简单的方法是eclipse链接,但它给了我错误,有人可以指导我如何纠正这个或任何其他更简单的方法来实现这一点。

感谢。

1 个答案:

答案 0 :(得分:0)

我们需要添加以下罐子,它会起作用:

1. jaxb-core_2.2.11.v201407311112.jar
2. jaxb-xjc_2.2.11.v201407311112.jar