我正在使用 Java 创建客户端 API:
+ Apache Jena FrameWork
+ Hydra(用于超媒体驱动)
+ 我的私有(private)词汇类似于 Markus Lanther Event-API Vocab而不是 schema.org(本体/词汇部分)
第 1 部分:
看完这个Markus Lanther EventDemo repo和 hydra-java 。我发现他们正在为每个九头蛇创建类:将来可能会破坏客户端的类。例如:
Person 类 (Person.java)
public class Person
{
String name;
};
但将来的需求名称也是一个类,例如:
public class Name
{
String firstName;
String LastName;
};
因此,为了满足此要求,我必须像这样更新 Person 类:
public class Person
{
Name name;
};
问题1:
我对本节的理解是否正确?如果是的话,这部分该如何处理?
第 2 部分:
为了避免上述问题,我创建了一个 GenericResource 类(GenericResource.java)
public class GenericResource
{
private Model model;
public void addProperty(String propertyName,Object propertyValue)
{
propertyName = "myvocab:"+propertyName;
//Because he will pass propertyName only eg: "name" and I will map it to "myvocab:name"
//Some logic to add propertyName and propertyValue to model
}
public GenericResource retriveProperty(String propertyName)
{
propertyName = "myvocab:"+propertyName;
//Some logic to query and retrieve propertyName data from this Object add it to new GenericResource Object and return
}
public GenericResouce performAction(String actionName,String postData)
{
//Some logic to make http call give response in return
}
}
但我再次遇到了很多问题:
问题1:
没有必要将每个propertyName都映射到myvocab:propertyName。
有些可能会映射到其他一些vocab,例如:Hydra:propertyName、schema:propertyName、 rdfs:propertyName、newVocab:propertyName 等
问题2:
如何验证这个propertyName是否属于这个类?
建议:
将类型字段/变量放在 GenericResource 类中。然后检查该类对应的 vocab 中的supportedProperty。
为了更清楚地假设上面的 Person 类,它也在 vocab 中定义并且具有supportedProperty:[名称,年龄等]。
所以我的GenericResource类型为“Person”,并且在addProperty或其他操作时,我将通过vocab查询该属性是否在supportedProperty列表或supportedOperation列表中在执行Action()的情况下。
这是正确的方法吗?还有其他建议将受到欢迎吗?
最佳答案
Question 1: Is my understanding correct or not of this Section? If yes then what is the way to deal with this part ?
是的,这似乎是正确的。仅仅因为 Hydra-java 决定创建类并不意味着您必须在实现中执行相同的操作。我宁愿编写一个映射器并注释一个可以保持稳定的内部类(您需要更新映射)。顺便说一句,您的 GenericResource 方法看起来也不错。
Problem 1: It is not necessary that every propertyName is mapped to myvocab:propertyName. Some may be mapped to some other vocab eg: hydra:propertyName, schema:propertyName, rdfs:propertyName, newVocab:propertyName, etc.
为什么不存储和访问具有完整 URL(即包括词汇)的属性?您当然可以实现一些方便的方法来简化您的词汇的工作。
Problem 2: How to validate whether this propertyName belongs to this class
Suggestion: Put type field/variable in GenericResource class
JSON-LD 节点对象中的 @type
(不在 @value
对象中)对应于 rdf:type
。因此,只需将其添加为所有其他属性即可。
And then check supportedProperty in vocab corresponding to that class.
请记住,supportedProperty
仅告诉您已知支持哪些属性。它不会告诉你哪些不是。换句话说,对象/资源上除 supportedProperty
列出的属性之外的属性也是有效的。
关于java - 在 java 中创建基于 JsonLd + Hydra 的通用客户端 API。有没有什么项目可以引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975128/