Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


GWT JSON integration with Spring MVC


The integration of JSON service in the backend with Spring MVC is quite similar to the integration of RPC services with Spring plain service beans. What’s new here is the introduction of Spring MVC to drive the JSON service.

 
Most of this integration is about smart configuration of the servlet mapping and the Spring MVC pipeline.

 

A Spring MVC configuration
Let’s see how we configure Spring MVC for this purpose:


 

    
        
                
        

    


 

    

 

    
    

 

Yes, that’s pretty few! Have you noticed that we did not define any viewResolver? It’s perfectly normal since we’re only using Spring MVC to produce JSON data back, there is no need for a view rendering technology.

Please notice that I prefer full XML declaration over namespace because we have greater control with full XML config. With namespace lots of things (message converters, exception handlers …) are injected by default (convention over configuration) and we do not need them for our usecase.

 

B web.xml configuration
The configuration for web.xml is very classical

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

 
  
    
        springMVCServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:stockwatcher_json-mvc.xml
        

        1
    

 
  
  
    springMVCServlet
    *.json
  

 

First we declare the Spring applicationContext as usual.

Then we declare the Spring MVC servlet with its associated configuration file (line 16)

Finally we map the MVC servlet to the .json extension (line 24). All request finishing with .json will trigger the Spring MVC servlet. The dispatching to service bean is handled by Spring MVC controller @RequestMapping annotation.
 

C MVC Request mapping
 
@RequestMapping(value = "**/jsonStocPrices.json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public List getStocks(@RequestParam("q") String rawSymbols)
{
    return this.jsonStockDataService.getStockDataFromSymboles(rawSymbols);
}
 
@RequestMapping(value = "**/jsonRandomize.json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Integer randomize()
{
    return this.jsonRandomizeService.getRandomNumber();
}
 

The request mapping in the MVC controller is quite straightforward. However you should pay attention to the request URL. Here we must use the **/ wildcard because we declared the servlet mapping as *.json.

Indeed if the absolute URL is /StockWatcherJSON/jsonStockPrices.json and the declared URL in @RequestMapping is just jsonStockPrices.json, it will not match beacause of the leading part (/StockWatcherJSON) of the URL.

asked Sep 1, 2015 in spring framework by rajesh
0 votes
51 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...