Reputation: 97
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?
Upvotes: 1
Views: 1294
Reputation: 20140
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")
Upvotes: 2