【问题标题】:Jibx Compilation not happeningJibx 编译没有发生
【发布时间】:2014-03-14 14:09:17
【问题描述】:

我正在尝试使用 JibX 将 XML 转换为 POJO。这是我的主要方法:

IBindingFactory bfact = BindingDirectory.getFactory(Asset.class);   
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

FileInputStream inputStream = new FileInputStream("NewSample.xml");
Object obj = uctx.unmarshalDocument(inputStream, null);
Asset asset = (Asset)obj;

在尝试这样做时,我遇到了这样的异常:

Unable to access binding information for class com.adaequare.model.document.Asset
Make sure the binding has been compiled

经过一番谷歌搜索后,我发现了问题所在。绑定必须在 JiBX 绑定编译器的帮助下编译成类文件。在 Jibx 站点中提到 org.jibx.binding.Compile 执行此绑定。所以我修改了我的主要方法是这样的:

package com.adaequare.client;
import org.jibx.binding.Compile;
import org.jibx.binding.Utility;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;

import com.adaequare.model.document.Asset;
import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class JibxTest {
    public static void main(String[] args) throws IOException { 
        try {
            Compile compiler = new Compile();
            compiler.compile(Utility.getClassPaths(), new String[]{"src/main/resources/jibx/AssetBinding.xml"});

            IBindingFactory bfact = BindingDirectory.getFactory(Asset.class);   
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

            FileInputStream inputStream = new FileInputStream("NewSample.xml");
            Object obj = uctx.unmarshalDocument(inputStream, null);
            Asset asset = (Asset)obj;
        } catch (Exception e) {   
            e.printStackTrace();
        }
    }
}

在运行此程序时,我收到以下异常:

Error running binding compiler
java.lang.NullPointerException
    at org.jibx.binding.classes.MungedClass.checkDirectory(MungedClass.java:241)
    at org.jibx.binding.classes.BoundClass.getGenericMunge(BoundClass.java:468)
    at org.jibx.binding.classes.BoundClass.getInstance(BoundClass.java:417)
    at org.jibx.binding.classes.BoundClass.getInstance(BoundClass.java:501)
    at org.jibx.binding.def.ObjectBinding.<init>(ObjectBinding.java:294)
    at org.jibx.binding.def.BindingBuilder.unmarshalObjectBinding(BindingBuilder.java:1150)
    at org.jibx.binding.def.BindingBuilder.unmarshalMapping(BindingBuilder.java:1888)
    at org.jibx.binding.def.BindingBuilder.unmarshalMappings(BindingBuilder.java:1243)
    at org.jibx.binding.def.BindingBuilder.unmarshalBindingDefinition(BindingBuilder.java:2245)
    at org.jibx.binding.Utility.loadBinding(Utility.java:300)
    at org.jibx.binding.Utility.loadFileBinding(Utility.java:420)
    at org.jibx.binding.Compile.compile(Compile.java:217)
    at com.adaequare.client.JibxTest.main(JibxTest.java:31)

当我检查 MungedClass 源代码时,它实际上是在寻找名称以“JiBX_”开头的已编译类文件。由于没有符合条件的文件,因此生成的文件数组为空。并且在访问数组长度时,会抛出 NPE。

我包含 compiler.compile 的目的是运行 Jibx 绑定编译器。但它仍然希望文件已经被它编译。我在这里遗漏了什么吗?

作为运行主类的替代方案,我也在 build.gradle 中定义了它。就是这样:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'jetty'

sourceCompatibility = 1.7
version = 1.0

repositories {
    mavenCentral()
}

ext.springVersion = '4.0.2.RELEASE';
ext.jerseyVersion = '2.6';
ext.jettyVersion = '9.1.3.v20140225';
ext.jibxVersion = "1.2.5";
httpPort = 8099;

def springDependencies = ["org.springframework:spring-core:$springVersion",
    "org.springframework:spring-beans:$springVersion",
    "org.springframework:spring-context:$springVersion",
    "org.springframework:spring-web:$springVersion",
    "org.springframework:spring-webmvc:$springVersion",
    "org.springframework:spring-oxm:$springVersion"]

def jettyDependencies = ["org.eclipse.jetty:jetty-server:$jettyVersion",
    "org.eclipse.jetty:jetty-servlet:$jettyVersion"]

def jerseyDependencies = ["org.glassfish.jersey.containers:jersey-container-servlet:$jerseyVersion",
    'javax.ws.rs:javax.ws.rs-api:2.0']

def jibxDependencies = ["org.jibx:jibx-run:$jibxVersion",
    "org.jibx:jibx-bind:$jibxVersion",
    "org.jibx:jibx-extras:$jibxVersion"]

ext.libs = [
    springDependencies : springDependencies,
    jerseyDependencies : jerseyDependencies,
    jibxDependencies : jibxDependencies,
    guava: 'com.google.guava:guava:16.0.1',
    junit: 'junit:junit:4.11'
]

configurations { jibx }

dependencies {
    compile springDependencies;
    compile jibxDependencies;
    compile jerseyDependencies;
    compile("org.glassfish.jersey.ext:jersey-spring3:$jerseyVersion"){
        exclude group: 'org.springframework'
    };
    compile libs.guava;
    testCompile libs.junit;
    jibx libs.jibxDependencies;
}

task(type : JavaExec, 'generateBinding') {
    classpath = configurations.jibx + configurations.compile + sourceSets.main.runtimeClasspath
    main = 'org.jibx.binding.Compile'
    args = [
        'src/main/resources/jibx/AssetBinding.xml'
    ]
}

compileJava.doLast{
    tasks.generateBinding.execute();
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

在运行“gradle clean build”时,它也会抛出与上面相同的异常。不确定我在这里缺少什么?

【问题讨论】:

    标签: java jibx


    【解决方案1】:

    普拉桑斯, 为什么不让您的生活更轻松一些,从 JiBX 项目或我们的 github 站点中的一个简单示例开始。

    我建议你从这个例子开始:

    https://github.com/jibx/maven-plugin/tree/master/test-suite/base-binding-test

    这个项目中的例子有一个简单的程序来编组和解组 xml。

    祝你好运!

    不要

    JiBX 贡献者

    【讨论】:

    • 谢谢你的例子,唐。但在我们的项目中,我们使用的是 Gradle。我能够让示例与 Maven 一起运行。但是对于gradle,我面临着这个问题。所以认为调试 org.jibx.binding.Compile(因为它是在运行构建时引发异常的地方)可以为我提供更多信息。但我也坚持这一点。
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多