Skip to content
agilej edited this page May 25, 2016 · 8 revisions

Expose examples.

expose json primitives

    exposer.expose(1).withName("int");
    exposer.expose("hello").withName("string");
    exposer.expose(false).withName("bool");
    exposer.expose(3.0).withName("float");

will output json as

{
 "int":1,
 "string":"hello",
 "bool":false,
 "float":3.0
}

expose simple array with name

    List<Integer> values1 = Arrays.asList(1,2,3,4);
    int[] values2 = new int[]{5,6,7};

    exposer.expose(values1).withName("values1");
    exposer.expose(values2).withName("values2");

will output json as

{
 "values1": [1,2,3,4],
 "values2": [5,6,7]
}

expose simple array without name

    List<Integer> values1 = Arrays.asList(1,2,3,4);

    exposer.expose(values1);

will output json as

 [1, 2, 3, 4]

expose map with name

    Map map = new HashMap();
    map.put("one", 1);
    map.put("two", 2);

    exposer.expose(map).withName("data");

will output json as

{
  "data": {
     "one": 1,
     "two": 2
  }
}

expose map without name

    Map map = new HashMap();
    map.put("one", 1);
    map.put("two", 2);

    exposer.expose(map);

will output json as

 {
    "one": 1,
    "two": 2
 }

expose single entity with name

    Account account = new Account();
    
    exposer.expose(account).withNameAndMapping("account", AccountMappingEntity.class);

will output json as

 {
    "account": {
        "username": "xxx",
        "age":  20
    }
 }

expose single entity without name

    Account account = new Account();
    
    exposer.expose(account).withMapping(AccountMappingEntity.class);

will output json as

 {
    "username": "xxx",
    "age":  30
 }

expose iterable entities with name

    List<Account> accounts = ...;  // or Account[] accounts = ...
    
    exposer.expose(accounts).withNameAndMapping("accounts", AccountMappingEntity.class);

will output json as

 {
    "accounts": [
        {
            "username": "xxx",
            "age":  20
        },
        {
            "username": "xxx",
            "age":  30
        }
    ]
 }

expose iterable entities without name

    List<Account> accounts = ...;  // or Account[] accounts = ...
    
    exposer.expose(accounts).withMapping(AccountMappingEntity.class);

will output json as

   [
        {
            "username": "xxx",
            "age":  20
        },
        {
            "username": "xxx",
            "age":  30
        }
    ]

expose same entity with different mapping

You can use different EntityMapper for same entity with different scenarios.

  class SimpleAccountMapping implements EntityMapper<Account>{
        public void config(Account account, FieldExposer exposer, Environment env) {
            exposer.expose(account.getLogin()).withName("loginName");
            exposer.expose(account.getId()).withName("id);
        }
  }
  class DetailAccountMapping implements EntityMapper<Account>{
        public void config(Account account, FieldExposer exposer, Environment env) {
            exposer.expose(account.getLogin()).withName("loginName");
            exposer.expose(account.getAge()).withName("age");
            exposer.expose(account.getAvatar()).withName("avatar");

            //nested entity mapping
            exposer.expose(account.getProfile()).withNameAndMapping("profile", ProfileEntity.class);
        }
  }
  //expose
  exposer.expose(accounts).withNameAndMapping("accountSummary", SimpleAccountMapping.class);

  exposer.expose(accounts).withNameAndMapping("accountDetail", DetailAccountMapping.class);

nested entity mapping

You can use one entity mapper in another entity mapper, the above example shows how to use ProfileEntity in DetailAccountMapping