I am trying to convert an XML into POJO using JibX. And here is my main method:
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;
On trying to do this, I got an exception like this:
Unable to access binding information for class com.adaequare.model.document.Asset
Make sure the binding has been compiled
After some googling I found out the issue. The bindings have to be compiled into class files with the help of JiBX binding compiler. And in Jibx site it is mentioned that org.jibx.binding.Compile does this binding. So I modified my main method like this:
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();
}
}
}
On running this I am getting the following exception:
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)
When I checked the MungedClass source code, it is actually looking for compiled class files whose name starts with 'JiBX_'. As there are no files matching the criteria, the resulting file array is null. And as array length is being accessed, an NPE is thrown.
My point of including compiler.compile is to run Jibx binding compiler. But still it expects the files to be already compiled by it. Am I missing something here?
As an alternative to running main class, I have defined it in my build.gradle too. Here is it:
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'
}
On running 'gradle clean build', it is also throwing the same exception as above. Not sure what I am missing here?