Friday, August 10, 2012

Jackson JSON Views in Spring MVC

Problem: I have a pojo which can easily be serialzed to JSON using Jackson JSON, the requirement is to only serialize select fields of a POJO.

Learning: Due to lack of documentation and pages not being indexed by Google, nobody said that if i dont annotate a field, it will be serialized. One of the fields was being serialized and i had to debug code to realize that though Views work, Jackson/Spring would serialize a field that is not annotated.


Solution: Using Spring MVC 3.1.1 Release and Jackson 1.9.x


    @RequestMapping(value="/rest", method = RequestMethod.GET)
    public ModelAndView speakers() {
   
    ObjectMapper listViewMapper = new ObjectMapper();
    listViewMapper.setSerializationConfig(listViewMapper.getSerializationConfig().withView(Speaker.ListView.class));
   
    MappingJacksonJsonView jsonListView = new MappingJacksonJsonView();
    jsonListView.setObjectMapper(listViewMapper);
   
    return new ModelAndView(jsonListView, "speakers", eventService.listSpeaker());
    }


and here is the POJO that i would like to filter, note that short_bio is linked to a different view and will not be serialized. 

public class Speaker {

    @Id
    @GeneratedValue
    private Integer uid;

    private String knownAs;

    private String designation;

    private String companyName;

    private String shortBio;
    
    private String pictureLink;

    public interface ListView {};
    public interface DetailView extends ListView {};
    
    @JsonView(ListView.class)
public Integer getUid() {
return uid;
}

public void setUid(Integer uid) {
this.uid = uid;
}

@JsonProperty("known_as")
@JsonView(ListView.class)
public String getKnownAs() {
return knownAs;
}

public void setKnownAs(String knownAs) {
this.knownAs = knownAs;
}

@JsonView(ListView.class)
public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

@JsonProperty("company_name")
@JsonView(ListView.class)
public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

@JsonProperty("short_bio")
@JsonView(DetailView.class)
public String getShortBio() {
return shortBio;
}

public void setShortBio(String shortBio) {
this.shortBio = shortBio;
}

@JsonProperty("picture")
@JsonView(ListView.class)
public String getPictureLink() {
return (pictureLink == null)?"":pictureLink;
}

public void setPictureLink(String pictureLink) {
this.pictureLink = pictureLink;
}
    
    
}


I am yet to try moving the View to a class so that i can be re-used across multiple POJO

Tuesday, August 7, 2012

Grails vs Play vs Ruby on Rails

This is a good comparison of Grails vs Play

http://www.ubertracks.com/preso/index.html#/


Grails vs Play vs Ruby on Rails (i know it is not Java, but the net is swamped by this)

this is a biased article on RoR
http://ruby.dzone.com/articles/rails-vs-grails

A good post with stats comparing JSF , Struts, Groovy, Ruby, Perl and Python
http://www.expectationgap.com/posts/13


I am learning Ruby now !! , the web and google is biased towards Ruby on Rails, i drank the cola

Ruby sucks, i am one of those lazy enterprise programmers who still loves Java. Spring MVC rocks !!


Update: After realizing that if so many startups are using Ruby on Rails, there should be some credit to it, hence focused and tried Ruby and RoR and finally was able to get an app running with CRUD, oh boy !! Ruby on Rails Rocks !!.


Friday, August 3, 2012

Google Technology Day Bangalore, Aug 3, 2012

Executive Summary: "the best ever technology conference that i have ever attended, all code and no bull shit"



KeyNote started with how lucky we where as, we were the choosen 1200 out of 3800 requests (too low for such an event, may be since this is the first time in Bangalore). Sunil Rao , Country Head for outreach program was in a cool shirt with jeans (i was the dumb guy in formals, my Goldman attire that i wore for 7.5 yrs could not be forgotten easily) and talked about how Google innovates and threw some numbers around why google thinks Mobile is the way to go, and in particular why mobile in india is a potential that is yet to be tapped.



Then we went into a demo session, where a googler talked about how google help us in our every day life, the demo of JellyBeans seems to excite all the college grads (60% of  attendees where college folks) but for a proud owner of iPhone, i could relate it to Siri.



Then we went to Google API which is the new service offered by Google for its Maps, Cal, Mail, Docs and Hangout the presenter was full of enthu and showed how simple it was to register a project and start using Google API, he also showed how to authenticate with OAuth 2, this was something that i wanted to read this weekend and having somebody explaining this live and being able to ask questions on it was a blessing.


Then attended another not so worth mentioning session, since the product was still in beta and we broke for lunch. Lunch as good and they had planned really well to manage such a huge crowd.

After lunch it was time to click some Google snaps , but the spot was so busy, but i never gave up.






at last !!





and play "Bambaram (Tamil)", i had never got it right duing my childhood days, but today it just came naturally. I was amazed.




Then went on to attend a session on Hangout API's taken by a new googler. Not so interesting.

The key attraction was presentation on HTML 5, the session was presented by the same enthu guy (Neelesh Bodas) who did the Google API, he had laid out the material very well and was able to engage the audience.

HTML 5 was also another buzz word that i wanted to read about, and he gave the best and attractive intro to it. Though most of the demo's were from internet, the packaging and presentation was cool.

Demos were from http://www.html5rocks.com/


Other References

https://sites.google.com/site/gindia12/agenda-presentations/technology-days

Follow GIndia updates in G+
https://plus.google.com/u/0/s/%23gindia

Thursday, August 2, 2012

Expose COD Views as REST Service in Drupal

End User Steps


1. Download Drupal Services 3.x and install the module (install is a big word, install = download zip file and unzip to sites/all/modules folder, create modules folder if required). Choose 6.x-x.x versions, since we are using Drupal 6
2. Install Services Views
3. Install oauth 6.x-3.0-beta4
4. Install autoload 6.v-2.1
5. Go to Administer->Site Building->Modules to enable the above modules, oauth might ask you to manually download spyc, follow instructions shown in the top of the screen when you enable it. It is as simple as copying the spyc.php to the rest_server/lib folder
6. Create new Service and test your rest service


References
Drupal Pro Guide to setup Views as Service
http://www.jefflinwood.com/2010/08/using-drupals-views-as-a-json-web-service-with-the-rest-server/


How add/install modules in Drupal
http://drupal.org/node/120641