I have a requirement to get JSON
input Pojo instance and I am using Jackson 2
library and below readValue
method could deserialise using typeReferencing :
POJO_ClassName p = mapper.readValue(new TypeReference< POJO_ClassName >() {});
But the problem is that as POJO
is created and loaded at runtime dynamically, how do I get JSON
to POJO
instance/object as I do not have fully qualified class (POJO_ClassName) name for above statement?
Note: I use jsonSchema2pojo
library to generate POJO
classes at runtime.
Here is code snippet, I am using to generate POJO
for JSON
at runtime
and trying
String classPath="com.EnrichmentService.Thread72";
String classLocation = System.getProperty("user.dir")
+ "/src/main/java"; JCodeModel codeModel = new JCodeModel();
final RuleFactory ruleFactory = new RuleFactory(config,
new Jackson2Annotator(config), new SchemaStore());
final SchemaMapper mapperSchema = new SchemaMapper(ruleFactory,
new SchemaGenerator());
mapperSchema.generate(codeModel, "EsRootDoc",classPath, json);
codeModel.build(new File(classLocation)); // generates pojo classes
// Till above jsonSchema2Pojo pojo generation all Good !!
// EsRootDoc instance is needed for further drools drl validations.
com.EnrichmentService.Thread72.EsRootDoc p = mapper.readValue(new TypeReference<com.EnrichmentService.Thread72.EsRootDoc>() {});
// see alternative way as well in my 24Aug17 edit at the end of this question
But as com.EnrichmentService.Thread72.EsRootDoc
has yet not been generated compiler would error to class not Found.
Main Points:
1) Same Pojo classes generated at run time iteratively but with different properties as JSON input changes each time.
2) Even tried Object pojo =mapper.readValue(json,Class.forName("com.EnrichmentService.Thread72.EsRootDoc")); as class.forName does not replace an existing class!
Edit 24 Aug17 - Here is my custom class loader :
Note: Indexer is class which load dynamic EsRootDoc/POJO class at run time.
static class TestClassLoader extends ClassLoader {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.equals("com.EnrichmentService.Thread72.EsRootDoc")) {
try {
InputStream is = Indexer.class.getClassLoader().getResourceAsStream("com/EnrichmentService/Thread72/EsRootDoc.class");
byte[] buf = new byte[is.available()];
int len = is.read(buf);
Class<?> c=defineClass(name, buf, 0, len);
resolveClass(c);
return c;
} catch (IOException e) {
throw new ClassNotFoundException("", e);
}
}
return getParent().loadClass(name);
}
}
I have tried using above TestClassLoader custom class loader as an alternative way is like this :
Class cls = new TestClassLoader().loadClass("com.EnrichmentService.Thread72.EsRootDoc");
Object obj = cls.newInstance();
cls.getMethod("getCrawlerSource").invoke(obj);
p=mapper.readValue(json, cls); // but here i am getting the same deserialization exception as earlier.
Referred an old answer@ How to replace classes in a running application in java ?
Edit2: 24Aug17 Exception being faced stackTrace is here: https://pastebin.com/ckCu2uWx
Imo there are two approaches to solve that:
or
you do something like that:
If that code fails before mapper.readValue() you have another problem (my guess would be with classloading).
Even better would be something with generics:
Edit:
I wrote some example code which compiles a class on runtime and then tries to convert to an object of said compiled class. The output was as I expected:
Edit 2:
Updated the code to try your TestClassLoader class - still get the correct (updated) version of the class.
you have found, you can only use
TypeReference
with types that are known at compile time (without some very tricky meta-programming).However, there are lots of alternative overloads to
readValue
which do not require aTypeReference
, to allow for cases like yours whereTypeReference
is impractical.I think you can use
readValue(... , Class<T> valueType)
If you have a special Classloader for these late-compiled classes, then you can get a
Class
instance from that and pass it in, for example:See also com.fasterxml.jackson.databind.type.TypeFactory for specifying parameterised generic types without using
TypeReference
Update after "Edit2: 24Aug17 Exception being faced stackTrace is here"
Your current exception ( https://pastebin.com/ckCu2uWx ) is not a class loader issue, but a JSON schema mismatch issue.
The relevant part of the exception message is:
So Jackson is unhappy that the "crawler" field in the JSON is an object, i.e. starts with "
{
", but the Java property "crawler" is an ArrayList, i.e. should start with "[
"I don't know why the POJO structure of
Thread72.Category
is wrong here, but it doesn't look like a classloader problem.Update after commenting that the POJO class changes with each request
You have said that 1) the POJO class structure varies with each request and 2) you want to use the same classname for the POJO each time.
See Java - how to load different versions of the same class?
You'll need to use a new Classloader for each request, as classes get cached by Classloaders. The code you have posted so far suggests that you are using a single classloader and hoping for a new load of the "Category" class on each request, which won't work.
You have said:
... but I think you should look into using a Map or similar input to Drools rather than a reflection based POJO, given that you don't know the structure in advance. As you have found here, this is a poor fit for the class / classloader abstraction.
See e.g.
Yes, of course. If you don't have class in your JVM, you can't load them.
Please, give all stacktrace.
Why you don't use Maps? Why you don't use one big class with all fields (and some of them will be nulls)?
If you do this in multiple threads, just synchronized them, for example:
If you need more strong consistency, then you can use synchronization by class name:
Method Class.forName uses class loader of caller. You use different classloaders, it could be reason. There are overloads, where you can pass classloader.
Update for stackrace and additional information.
You should add @JsonIgnoreProperties(ignoreUnknown = true) to Crawler, because it has field subCategory, which is not present in Pojo.