How to create a Backbone model from POJO or JavaBean?

I have a POJO called Person.java, is there a bash or utility that allows me to create a Backbone model called person.js from Person.java, so that I don’t have to enter all the fields again?

Thank.

+3
source share
2 answers

If you use the Jackson JSON processor http://jackson.codehaus.org/ to translate the POJO model code into JSON, you do not need to recreate any properties on your base model. A simple example:

public String getPerson(){
    Person personPOJOInstance = new Person();
    ObjectMapper mapper = new ObjectMapper();
    StringWriter sw = new StringWriter();

    try{
        mapper.writeValue(sw, personPOJOInstance);
        pojoJSON = sw.getBuffer().toString();
    }
    catch(IOException exc){

    }
    return pojoJSON;
}

You don’t even have to worry about this if you use a Spring MVC controller and mark your controller method with the following @RequestMapping annotation, for example:

@RequestMapping(method= RequestMethod.GET, produces = "application/json", value="/path/to/controller/method")
public @ResponseBody getPerson(){
    return new Person();
}

, :

var Person = Backbone.Model.extend({
    url: '/path/to/controller/method'
});

- Backbone, .

, , , POJO Backbone, :

//instantiate and fetch your model.
var person = new Person();
person.fetch();
...
//access properties on your model.
var name = person.get('name');
+5
+2

All Articles