Jul 3, 2011

Spring MVC JSON example


One of the person was asking me to post a Spring MVC JSON example in stackoverflow. I am demonstrating only the code on the fly, not in formal way.

public ModelAndView JsonExample(HttpServletRequest request,
   HttpServletResponse response) throws Exception {

  HashMap  map = new HashMap ();
  String pattern = request.getParameter("tag");
  ArrayList < Map < String , Object > > list = distributorService.getCustomerList();
  

  if (pattern != null && !pattern.equalsIgnoreCase("")) {
   ArrayList < Map < String , Object > > list1 = new ArrayList < Map < String , Object > >();
   for (int i = 0; i < list.size(); i++) {
    
    
    if ((((String) list.get(i).get("caption")).toLowerCase()).contains(pattern.toLowerCase()))
     list1.add(list.get(i));
    
   }
   map.put("customerList", list1);

  } else {
   map.put("customerList", null);

  }
  

  return new ModelAndView(new JSONView(), map);
 }
And then create a service interface and implement that interface:
public ArrayList < Map < String , Object > > getCustomerList(){
  List list = distributorDao.getCustomerList();
  ArrayList < Map < String , Object > > listN = new ArrayList < Map < String , Object > >();
  Map < String , Object > info =null;
  String type="";
  
  for(int i = 0;i < list.size();i++){
   info = new HashMap < String , Object >();
   Object[] obj = (Object[]) list.get(i);
   info.put("value", obj[0]);
   info.put("caption", obj[1]);
   listN.add(info);
  }
  return listN;
 }
And later add the DAO interface and implement that:
public List getCustomerList(){

  String queryStr = "select customerid,customer_name from customer_tbl";
  Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
  Query query = session.createSQLQuery(queryStr);
  List list = query.list();
  
  return list;
 }





--- Hossain Doula

3 comments:

  1. Hi, its the guy from stackoverflow. Thanks again for your example.
    But which JSON library do you use?

    ReplyDelete
  2. My code:
    HashMap map = new HashMap();
    map.put("isAuthenticated", false);
    map.put("isAuthorized", false);
    map.put("hasErrors", false);

    try {
    initialize();

    if (currentUser != null) {
    map.put("isAuthenticated", true);

    if (currentUser.isSuperAdministrator()) {
    map.put("isAuthorized", true);
    map.put("data", usersService.getCountry(id));
    }
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }

    return new ModelAndView(new JsonView(), map);

    But it fails on StackOverflowError in JSONObject. Can you tell me where is the problem? I want to get simple JSON object like {isAuthenticated: true, isAuthorized:false, hasErrors:false, data:{id:1,...}}

    ReplyDelete
  3. I figured out where the problem - when I pass an hibernate object (usersService.getCountry(id)). It hasn't any LAZY-collections. Here it is:
    public class Country2 implements Serializable {
    /**
    *
    */
    private static final long serialVersionUID = -1275432204448859586L;

    /**
    *
    */
    @Id
    @Column(name = "id")
    private Long id;

    /**
    *
    */
    @Column(name = "version")
    private Integer version;

    /**
    *
    */
    @Column(name = "typecode")
    private Integer type;

    /**
    *
    */
    @Column(name = "lastmodifiedby")
    private String lastModifiedBy;

    /**
    *
    */
    @Column(name = "lastmodifieddate")
    private String lastModifiedDate;

    /**
    *
    */
    @Column(name = "country_de")
    private String nameDe;

    /**
    *
    */
    @Column(name = "country_en")
    private String nameEn;

    /**
    *
    */
    @Column(name = "country_ru")
    private String nameRu;

    /**
    *
    */
    public Country2() {
    this.setId(0L);
    }

    public void setId(Long id) {
    this.id = id;
    }

    public Long getId() {
    return id;
    }

    public void setVersion(Integer version) {
    this.version = version;
    }

    public Integer getVersion() {
    return version;
    }

    public void setType(Integer type) {
    this.type = type;
    }

    public Integer getType() {
    return type;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
    this.lastModifiedBy = lastModifiedBy;
    }

    public String getLastModifiedBy() {
    return lastModifiedBy;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
    this.lastModifiedDate = lastModifiedDate;
    }

    public String getLastModifiedDate() {
    return lastModifiedDate;
    }

    public void setNameDe(String nameDe) {
    this.nameDe = nameDe;
    }

    public String getNameDe() {
    return nameDe;
    }

    public void setNameEn(String nameEn) {
    this.nameEn = nameEn;
    }

    public String getNameEn() {
    return nameEn;
    }

    public void setNameru(String nameRu) {
    this.nameRu = nameRu;
    }

    public String getNameRu() {
    return nameRu;
    }
    }

    ReplyDelete