Skip to content
pdd edited this page Jul 9, 2011 · 3 revisions

Any document has fields which are the name-value pairs stored in mongoDB. This page shows how you can define fields in service definition.

"fields" : [
    {
        "name" : "make",
        "type" : "String"
    },
    {
        "name" : "model",
        "type" : "String"
    },
]

Valid types

String
boolean
int
long
double
byte[]
Date
Object    
List

Valid List types

List<byte[]>
List<Boolean>
List<Double>
List<Integer>
List<Long>
List<String>
List<Date>
List<Long>
List<Map>

Object type

A field can be any arbitrary JSON object. This maps with "Object" type.
When a type is specified as an "Object", it must be followed by "fields" which defines the fields for this embedded object.
Objects may be arbitrarily embedded.
An embedded class is generated for an embedded object.

For example, you can look at User definition in samples. A field "info" is defined as "Object" type.

{
    "name" : "info",
    "type" : "Object",
    "fields" : [
        {
            "name" : "dob",
            "type" : "Date"
        },
        {	
            "name" : "phone",
            "type" : "String"
        }
}

The generated embedded interface and class can be accessed as User.Info and UserImpl.InfoImpl.

List of Objects

You may have a List of arbitrary Objects defined as shown below.

{
    "name" : "history",
    "type" : "List<RegInfo>",
    "fields" : [
        {
            "name" : "regDate",
            "type" : "Date"
        },
        {
            "name" : "mileage",
            "type" : "int"
        },
        {
            "name" : "state",
            "type" : "String"
        }
    ]
}

This will generate static interface and class definitions which can be accessed from the parent model. For example, this one is defined in Car service under samples and can be accessed as Car.RegInfo and CarImpl.RegInfoImpl.

External or Custom List Types

{   "name" : "likedCars",
    "type" : "List<String>"
},
{   "name" : "followedUsers",
    "type" : "List<Long>"
},
{
    "name" : "someList",
    "type" : "List<Map<String, Object>>"
},
{
    "name" : "registeredDrivers",
    "type" : "List<com.example.demo.model.RegisteredDriver>"
}

The first 3 List types will work w/o any issues.
RegisteredDriver is a custom model type not generated by ServiceBuilder.
In other words, it is not defined in the service definition (hence external).
This needs some special handling because even though RegisteredDriver might be serializable but BSON serialization fails. So do the following to overcome that issue.

External List type MUST

  1. Have a constructor to build object from Map<String, Object>
    • public RegisteredDriverImpl(Map<String, Object> map) {...}
  2. Have toMap method defined in the interface and implemented in class
    • public Map<String, Object> toMap();
  3. Have implementation of interface in impl package
    • interface - com.example.demo.model.RegisteredDriver
    • class - com.example.demo.model.impl.RegisteredDriverImpl

Clone this wiki locally