为多态关联指定生成的JSON的输出属性

时间:2017-12-20 15:05:37

标签: javalite activeweb

我使用多态关联将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类中的子节点以映射到上面的JSON?

以下是我想要映射JSON的java类的内容(我省略了访问器方法):

@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 个答案:

答案 0 :(得分:1)

您可以通过以下两种方式之一完成此操作:

  1. 在模型上编写getter方法,并使用Jackson生成JSON而不是依赖于内置机制。
  2. 使用ActiveWeb视图和部分来生成JSON。
  3. 我个人选择第二种方法,因为它已经内置到框架中,并且是最灵活和可读的。

    我们说你需要归还人和他们的地址。 这是您的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。系统会自动为集合中的每个值调用此内容,其内容将由部分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集合使用相同的技术,只使用集合address调用person.getAddresses()部分。 comma部分的内容是单个字符:,

    您可以在JavaLite / ActiveWeb中检查/运行示例应用程序REST服务构建: https://github.com/javalite/activeweb-rest/tree/master/src/main/webapp/WEB-INF/views/people

    如果您运行此示例应用程序并按照自述文件中的步骤操作,您将在输出中看到此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"
          }
        ]
      }
    ]
    

    我希望它有所帮助