我想编写一个 Restful API 并使用 schema.org 注释我的数据。为此,我想使用 Jackson-Jsonld。使用 jackson-jsonld 注释简单对象没有问题,但具有嵌套对象的复杂对象让我陷入困境。在我的 jsonld 中,像 id、name 这样的简单属性被注释,但嵌套位置没有注释。
我读到了有关序列化的内容,它应该有助于获取第二个对象。然而,在实现我的序列化部分之后,序列化似乎没有改变任何东西。 这是我的示例输出,位置的类型应该是 PostalAddress,但是缺少类型:
{"@context":
{"uri":"http://schema.org/url","name":"http://schema.org/name","location":"http://schema.org/location"},
"@type":"http://schema.org/Organization",
"uri":"http://localhost:8080/kangarooEvents/venue/12",
"name":"Joondalup Library - Ground Floor Meeting Room",
"location":{
"address":"102 Boas Avenue",
"city":"Joondalup",
"zip":"6027",
"country":"Australia",
"state":"WA"},
"@id":12}
我想注释一个具有单一位置的组织:
@JsonldType("http://schema.org/Organization")
public class Venue {
@JsonldId
private Integer id;
@JsonldProperty("http://schema.org/url")
private String uri;
@JsonldProperty("http://schema.org/name")
private String name;
@JsonSerialize(using = CostumLocationSerializer.class)
@JsonldProperty("http://schema.org/location")
private Location location;
地点:
@JsonldType("http://schema.org/PostalAddress")
public class Location {
@JsonldProperty("http://schema.org/streetAddress")
private String address;
@JsonldProperty("http://schema.org/addressLocality")
private String city;
@JsonldProperty("http://schema.org/addressRegion")
private String state;
@JsonldProperty("http://schema.org/addressRegion")
private String country;
@JsonldProperty("http://schema.org/postalCode")
private String zipcode;
序列化:
public class CostumLocationSerializer extends StdSerializer<Location> {
private ObjectMapper mapper = new ObjectMapper();
public CostumLocationSerializer(){
this( null);
}
protected CostumLocationSerializer(Class<Location> t) {
super(t);
}
@Override
public void serialize(Location location, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("address", location.getAddress());
jsonGenerator.writeStringField("city", location.getCity());
jsonGenerator.writeStringField("zip", location.getZipcode());
jsonGenerator.writeStringField("country", location.getCountry());
jsonGenerator.writeStringField("state", location.getState());
jsonGenerator.writeEndObject();
String serialized = mapper.writeValueAsString(location);
}
}
我认为我的问题可能出在序列化上,但我无法弄清楚。也许有人注释了嵌套 obj。并可以告诉我我的问题是什么。
最佳答案
只需跳过jackson-jsonld
部分并手动执行
- 创建 JSON - 只需在 Java 类中引入
type
和id
字段即可。 - 创建 JSON-LD 上下文 - 将您的
id
和type
字段映射到另一个@context
对象中 - 结合上下文和数据 - 例如只需使用标准 jackson API 在“正常”json 序列化之后添加您的
@context
对象即可。
示例
@Test
public void createJsonFromPojo() throws Exception {
ObjectMapper mapper=new ObjectMapper();
// Create object structure
Venue venue = new Venue();
venue.location = new Location();
venue.id="12";
venue.uri="http://localhost:8080/kangarooEvents/venue/12";
venue.name="Joondalup Library - Ground Floor Meeting Room";
venue.location.address="102 Boas Avenue";
venue.location.city="Joondalup";
venue.location.state="WA";
venue.location.country="Australia";
venue.location.zipcode="6027";
//1. Create JSON
ObjectNode myData = mapper.valueToTree(venue);
//2. Create a JSON-LD context
ArrayNode context = mapper.createArrayNode();
context.add("http://schema.org/");
ObjectNode myContext=mapper.createObjectNode();
myContext.put("id", "@id");
myContext.put("type", "@type");
context.add(myContext);
//3. Combine context and data
myData.set("@context",context);
//4. Print
StringWriter w = new StringWriter();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, myData);
String result= w.toString();
System.out.println(result);
}
public class Venue {
public final String type = "http://schema.org/Organization";
public String id;
public String uri;
public String name;
public Location location;
}
public class Location {
public final String type = "http://schema.org/PostalAddress";
public String address;
public String city;
public String state;
public String country;
public String zipcode;
}
给你
{
"@context": [
"http://schema.org/",
{
"id": "@id",
"type":"@type"
}
],
"uri":"http://localhost:8080/kangarooEvents/venue/12",
"name":"Joondalup Library - Ground Floor Meeting Room",
"location":{
"address":"102 Boas Avenue",
"city":"Joondalup",
"zip":"6027",
"country":"Australia",
"state":"WA",
"type":"http://schema.org/PostalAddress"
},
"id":"12",
"type":"http://schema.org/Organization"
}
关于java - Jackson-JsonLD 嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50738973/