Jackson-JsonLD嵌套对象

问题描述 投票:2回答:1

我想编写一个restful API并使用schema.org注释我的数据。为此,我想使用Jackson-Jsonld。使用jackson-jsonld注释简单对象没有问题,但是嵌套对象的复杂对象让我陷入困境。在我的jsonld中,id,name这样的简单属性得到了反转,但嵌套的位置却没有。

我读到了序列化,它应该有助于获得第二个对象。但是,在实现我的序列化部分后,似乎序列化没有改变任何东西。这是我的示例输出,location的类型应该是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。并告诉我我的问题是什么。

java jackson schema.org json-ld
1个回答
0
投票

跳过jackson-jsonld部分并手动完成

  1. 创建JSON - 只需将typeid的字段引入到您的java类中。
  2. 创建一个JSON-LD上下文 - 在另外的id对象中映射你的type@context字段
  3. 结合上下文和数据 - 例如只需在使用标准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"
}

View Example in Playground

© www.soinside.com 2019 - 2024. All rights reserved.