-
Notifications
You must be signed in to change notification settings - Fork 0
Expose Example
agilej edited this page May 25, 2016
·
8 revisions
Expose examples.
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
}
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]
}
List<Integer> values1 = Arrays.asList(1,2,3,4);
exposer.expose(values1);
will output json as
[1, 2, 3, 4]
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
}
}
Map map = new HashMap();
map.put("one", 1);
map.put("two", 2);
exposer.expose(map);
will output json as
{
"one": 1,
"two": 2
}
Account account = new Account();
exposer.expose(account).withNameAndMapping("account", AccountMappingEntity.class);
will output json as
{
"account": {
"username": "xxx",
"age": 20
}
}
Account account = new Account();
exposer.expose(account).withMapping(AccountMappingEntity.class);
will output json as
{
"username": "xxx",
"age": 30
}
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
}
]
}
List<Account> accounts = ...; // or Account[] accounts = ...
exposer.expose(accounts).withMapping(AccountMappingEntity.class);
will output json as
[
{
"username": "xxx",
"age": 20
},
{
"username": "xxx",
"age": 30
}
]
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);
You can use one entity mapper in another entity mapper, the above example shows how to use ProfileEntity
in DetailAccountMapping