1

I am currently learning about RDF and I am attempting to convert an RDF document into a Java Bean.

So far I have found and used the Jena library to read in an RDF Turtle document. I can then iterate over the statements within the Jena model and print out each statements subject, predicate and object. But instead I would like to convert/bind the RDF within the model to a Java bean.

Here is the code I am using to read the RDF Turtle into the Jena bean:

Model model = new ModelCom(new GraphMem());
model.read(new ByteArrayInputStream(body.getBytes()), null, "TURTLE");

Property predicate;
Statement statement;
Resource subject;
RDFNode obj;
StmtIterator iter = model.listStatements();

while(iter.hasNext()) {
    statement = iter.next();

    subject = statement.getSubject();

    System.out.println("Subject = " + subject.getURI());

    predicate = statement.getPredicate();

    System.out.println("Predicate = " +predicate.getLocalName());

    obj = statement.getObject();

    System.out.println("Object = " + obj.toString());

}

I have tried for a couple of days, but I cannot find any documentation anywhere that demonstrates how to bind a model to a Java bean.

Ideally what I would like to do is:

Person person = model.read(Person.class);
crmepham
  • 4,676
  • 19
  • 80
  • 155
  • Don't know how to achieve your ideal result, but you might find it easier to start with json-ld instead of turtle if you have to roll your own solution – chrisis Oct 16 '18 at 19:38
  • Unfortunately I am forced to use RDF Turtle. – crmepham Oct 16 '18 at 19:42
  • 1
    Possible duplicate of [How can I easily convert RDF triples to/from an idomatic Java POJO business object?](https://stackoverflow.com/questions/34098348/how-can-i-easily-convert-rdf-triples-to-from-an-idomatic-java-pojo-business-obje) – Jeen Broekstra Oct 16 '18 at 22:51

0 Answers0