簡體   English   中英

為多態關聯為生成的JSON指定輸出屬性

[英]Specify output attributes for generated JSON for polymorphic associations

我使用多態關聯將2個表鏈接到其常用表:

@BelongsToPolymorphic(parents = {Application.class, SiteSection.class})
public class Parameter extends Model {
    static {
        validatePresenceOf("parent_id", "parent_type");
    }
}

控制器中使用的查詢是這樣的:

String siteSettingsParameters = SiteSection.where("site_id=?", site.getId()).include(Parameter.class).toJson(false);

結果JSON包含子節點:

[
    {
        "id": 253,
        "section_id": 60,
        "site_id": 61,
        "children": {
            "parameters": [
                {
                    "created_at": "2017-12-19T15:50:28Z",
                    "id": 145,
                    "parent_id": 253,
                    "parent_type": "app.models.SiteSection",
                    "updated_at": "2017-12-19T15:50:28Z",
                    "value": "Terrible Swift Sword"
                }
            ]
        }
    },  
    {
        "id": 254,
        "section_id": 61,
        "site_id": 61,
        "children": {
            "parameters": [
                {
                    "created_at": "2017-12-19T15:50:28Z",
                    "id": 146,
                    "parent_id": 254,
                    "parent_type": "app.models.SiteSection",
                    "updated_at": "2017-12-19T15:50:28Z",
                    "value": "Sleep the Brave"
                }
            ]
        }
    },
...]

是否可以跳過 Java類中的children節點以映射到上述JSON?

這是我想映射JSON的java類的內容(我省略了accessors方法):

@JsonIgnoreProperties(ignoreUnknown = true)
public class SiteSectionDTO {
    private Integer id;
    private SectionDTO section;
    private SiteDTO site;

    @JsonProperty("children")
    private List<ParameterDTO> parameters;
...
accessors come here

謝謝。

您可以通過以下兩種方式之一執行此操作:

  1. 在模型上編寫getter方法,並使用Jackson生成JSON,而不是依賴於內置的機械機制。
  2. 使用ActiveWeb視圖和局部視圖生成JSON。

我個人選擇第二種方法,因為它已經內置在框架中,並且是最靈活和易讀的。

假設您需要返回人員及其地址。 這是您的PeopleController#index()方法:

public void index(){
   view("people", Person.findAll().include(Address.class).orderBy("id"));
   render().contentType("application/json");
}

如您所見,我們使用include()方法預先緩存地址。

index.ftl視圖如下所示:

[<@render partial="person" collection=people spacer="comma"/>]

現在,請注意這里。第一個和最后一個字符是[] ,它們是標識JSON中的數組的方括號。 內容是部分稱呼的person 對於集合“ people”中的每個值,將自動調用此方法,並且它們的內容將與部分comma的內容交織在一起。

讓我們看看部分person的內容:

{
   "id" : ${person.id},
   "first_name" : "${person.first_name}",
   "last_name" : "${person.last_name}",
   "addresses" : [<@render partial="address" collection=person.getAddresses() spacer="comma"/> ]
}

ActiveWeb自動具有一個可用的對象,該對象由部分本身的名稱命名,在本例中為person

在這里,您可以選擇要顯示的屬性和顯示順序。 addresses集合使用相同的技術,只是使用集合person.getAddresses()部分調用address comma部分的內容是單個字符: ,

您可以在JavaLite / ActiveWeb中檢查/運行示例應用REST服務構建: https : //github.com/javalite/activeweb-rest/tree/master/src/main/webapp/WEB-INF/views/people

如果運行此示例應用程序並按照README文件中的步驟進行操作,您將在輸出中看到此JSON:

[
  {
    "id": 1,
    "first_name": "Marylin",
    "last_name": "Monroe",
    "addresses": [
      {
        "address_type": "residential",
        "address1": "123 Pine St",
        "address2": "Apt 3",
        "city": "Chicago",
        "state": "IL",
        "zip": "60606"
      },
      {
        "address_type": "shipping",
        "address1": "135 S LaSalle St",
        "address2": "",
        "city": "Chicago",
        "state": "IL",
        "zip": "60604"
      }
    ]
  },
  {
    "id": 2,
    "first_name": "John",
    "last_name": "Kennedy",
    "addresses": [
      {
        "address_type": "residential",
        "address1": "456 Pine St",
        "address2": "Apt 5",
        "city": "Chicago",
        "state": "IL",
        "zip": "60606"
      },
      {
        "address_type": "shipping",
        "address1": "200 N LaSalle St",
        "address2": "",
        "city": "Chicago",
        "state": "IL",
        "zip": "60604"
      }
    ]
  }
]

希望對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM