1

I want to convert Java object to RDF XML. I am using Jena API. I don't want to call any REST call.

On REST method, we can write:

@Produces(OslcMediaType.APPLICATION_RDF_XML)

So it sends the response in RDF XML format.

I can't use this, because I am already in one REST method. I can call another REST call to convert an object to RDF. But I don't want to call another REST call.

Does anyone has another solution to convert Java object to RDF XML?

dur
  • 15,689
  • 25
  • 79
  • 125
rathod151
  • 97
  • 8
  • What is the serialization framework used by Jena? In the case of Jackson you could get access to the ObjectMapper and manually serialize the content yourself. I'd try looking there, but I don't have experience with Jena to tell you exactly. – Michael Hibay Apr 28 '17 at 14:58
  • this is basically a duplicate of http://stackoverflow.com/questions/34098348/how-can-i-easily-convert-rdf-triples-to-from-an-idomatic-java-pojo-business-obje?rq=1 – Michael May 01 '17 at 15:32

1 Answers1

2

You can create your own RDF Serializer for you class and you can use Jena API. Here is a very naive example:

public class Person {
    String name;
    int age;
    Person(String name){
        this.name = name;
    }
    String getIRI(){
        return "http://example.com/"+name;
    }
    String serialize(String syntax){
        Model model = ModelFactory.createDefaultModel();
        Resource resource = model.createResource(getIRI());
        // add the property
        resource.addProperty(FOAF.name, name);
        StringWriter out = new StringWriter();
        model.write(out, syntax);
        return out.toString();
    }
}

To serialize the class, call Person p1 = new Person("Noor");. p1.serialize("RDF/XML-ABBREV")

Noor
  • 19,638
  • 38
  • 136
  • 254