Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to POST and GET JSON between EXTJS and SpringMVC3

DZone's Guide to

How to POST and GET JSON between EXTJS and SpringMVC3

Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

After one month of evaluation of frameworks and tools, I choose ExtJS for UI and Spring/SpringMVC for the business layer for my pet project.

By using ExtJS we can send data to server by form submits or as request parameters, or in JSON format through Ajax requests. ExtJS uses JSON format in many situations to hold data. So I thought using JSON as data exchange format between EXTJS and Spring would be consistent.


The following code snippets explain how we can use ExtJS and SpringMVC3 to exchange data in JSON format.

1. Register MappingJacksonHttpMessageConverter in dispatcher-servlet.xml
  

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        
 <property name="messageConverters">
       <list>
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
       </list>
  </property>        
</bean>
   
Add jackson-json jar(s) to WEB-INF/lib
    
2. Trigger the POST request from ExtJS script as follows:
   
  
Ext.Ajax.request({
  url : 'doSomething.htm',
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },                        
  params : { "test" : "testParam" },
  jsonData: {
                   "username" : "admin",
                    "emailId" : "admin@sivalabs.com"
                 },
  success: function (response) {
                      var jsonResp = Ext.util.JSON.decode(response.responseText);
                      Ext.Msg.alert("Info","UserName from Server : "+jsonResp.username);
              },
  failure: function (response) {
                  var jsonResp = Ext.util.JSON.decode(response.responseText);
                  Ext.Msg.alert("Error",jsonResp.error);
            }
});
    
3. Write a Spring Controller to handle the "/doSomething.htm" reguest.

@Controller
public class DataController
{
    @RequestMapping(value = "/doSomething", method = RequestMethod.POST)
    @ResponseBody
    public User handle(@RequestBody User user) throws IOException
    {
        System.out.println("Username From Client : "+user.getUsername());
        System.out.println("EmailId from Client : "+user.getEmailId());
        user.setUsername("SivaPrasadReddy");
        user.setEmailId("siva@sivalabs.com");        
        return user;
    }
}

Any other better approaches?

From http://sivalabs.blogspot.com/2011/06/how-to-post-and-get-json-between-extjs.html

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}