Java Jackson JsonLD嵌套对象
我想编写一个restful API,并用schema.org注释我的数据。为此,我想使用Jackson Jsonld。用jackson jsonld注释简单的对象没有问题,但用嵌套对象注释复杂的对象让我感到受挫。在我的jsonld中,id、name等简单属性被注释,但嵌套位置没有注释 我读过关于序列化的内容,它应该有助于获得第二个对象。然而,在实现了我的序列化部分之后,序列化似乎没有改变任何东西。 这是我的示例输出,位置的类型应为PostalAddress,但缺少该类型:Java Jackson JsonLD嵌套对象,java,jackson,schema.org,json-ld,Java,Jackson,Schema.org,Json Ld,我想编写一个restful API,并用schema.org注释我的数据。为此,我想使用Jackson Jsonld。用jackson jsonld注释简单的对象没有问题,但用嵌套对象注释复杂的对象让我感到受挫。在我的jsonld中,id、name等简单属性被注释,但嵌套位置没有注释 我读过关于序列化的内容,它应该有助于获得第二个对象。然而,在实现了我的序列化部分之后,序列化似乎没有改变任何东西。 这是我的示例输出,位置的类型应为PostalAddress,但缺少该类型: {"@context"
{"@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);
}
}
公共类CostumLocationSerializer扩展StdSerializer{
私有对象映射器映射器=新对象映射器();
public CostumLocationSerializer(){
这个(空);
}
受保护的CostumLocationSerializer(t类){
超级(t);
}
@凌驾
public void serialize(位置、JsonGenerator JsonGenerator、SerializerProvider SerializerProvider)引发IOException{
jsongGenerator.writeStartObject();
jsonGenerator.writeStringField(“地址”,location.getAddress());
jsonGenerator.writeStringField(“city”,location.getCity());
jsonGenerator.writeStringField(“zip”,location.getZipcode());
jsonGenerator.writeStringField(“country”,location.getCountry());
jsonGenerator.writeStringField(“state”,location.getState());
jsongGenerator.writeEndObject();
String serialized=mapper.writeValueAsString(位置);
}
}
我想我的问题可能在序列化中,但我无法解决它。也许有人注释了嵌套obj。可以告诉我我的问题是什么 只需跳过jackson jsonld的
部分,手动执行即可
创建JSON-只需在java类中引入一个type
和id
字段李>
创建JSON-LD上下文-将您的id
和类型
字段映射到另一个@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"
}