Skip to content

QBit, Microservices REST lib, working with Map String, Object for types

Richard Hightower edited this page Sep 15, 2015 · 1 revision

When working with REST endpoints at times you want extra capability from one REST end point. To support polymorphic subtypes or operations, you may want to send a wrapper Map that gives some context information. The information could be about what types the sub-maps are.

Let's show a quick example of this using the TodoService from the previous examples. The complete code for this example can be found here Todo Map.

Example of using a Map<String, Object> instead of a strongly typed POJO


import io.advantageous.boon.core.reflection.MapperSimple;


    @RequestMapping(value = "/todo", method = RequestMethod.POST,
            description = "add a todo item to the list", summary = "adds todo",
            returnDescription = "returns true if successful")
    public boolean add(final Map<String, Object> todoMap) {

        String id = todoMap.get("id").toString();

        String name = (String)todoMap.get("name");


        String description = (String)todoMap.get("description");


        Long createTime = (Long)todoMap.get("createTime");


        Map<String, Object> parent = (Map<String, Object>) todoMap.get("parent");

        MapperSimple simple =  new MapperSimple();

        Category category = simple.fromMap(parent, Category.class);
        Todo todo = new Todo(name, description, createTime, category);

        todoMap.put(id, todo);

        return true;
    }

Notice that the add method now takes a Map<String, Object> instead of a Todo POJO. We can pull items out of the map, which equate to properties of a Todo item.

For this example, I changed the Todo to have a property of type Category.

New Property of type Category

@Description("A `TodoItem`.")
public class Todo {


    @Description("Holds the description of the todo item")
    private final String description;

    @Description("Holds the name of the todo item")
    private final String name;


    private  String id;

    private final long createTime;

    private final Category parent;

...

public class Category {

    private final String name;

    public Category(String name) {
        this.name = name;
    }
}

The category payload is expressed as another JSON object, aka, a Java Map Map<String, Object> parent = (Map<String, Object>) todoMap.get("parent");.

We can pull out the sub map which represents the category and use a MapperSimple to convert the map into a POJO (Category).

Converting a map to a POJO


        Map<String, Object> parent = (Map<String, Object>) todoMap.get("parent");

        MapperSimple simple =  new MapperSimple();

        Category category = simple.fromMap(parent, Category.class);

QBit has both a MapperSimple and a MapperComplex, which both implement Mapper. A Mapper converts maps into objects. The MapperComplex allows you to ignore properties, decide how to read fields, etc.

This example was kept fairly simple. You could imagine a more involved example where you read fields and then decide which POJO to instantiate from a list of subclasses.

You can’t perform that action at this time.