Spring MVC - JSON Mapping View

[Last Updated: Oct 25, 2017]

This example shows how to render Spring view in JSON format. Spring provides MappingJackson2JsonView to achieve that. By default, the entire contents of the model Map (populated in a controller method) will be rendered in JSON (except for the framework-specific objects).


Additional Maven Dependency



Java Config class

public class MyWebConfig {

  public ViewResolver beanNameViewResolver() {
      BeanNameViewResolver resolver = new BeanNameViewResolver();
      return resolver;

  public View jsonView() {
      MappingJackson2JsonView view = new MappingJackson2JsonView();
      return view;

  public ViewResolver jspViewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      return viewResolver;

In above configuration, we are using BeanNameViewResolver for resolving the JSON view by it's bean name.

Spring Controller

public class MyController {

  public String mainView() {
      return "main";

  public String handleForexRequest(Model model) {
      model.addAttribute("currencyRates", getCurrencyRates());
      return "currencyRateView";
public class CurrencyRate {
  private String currencyPair;
  private Date date;
  private BigDecimal askPrice;
  private BigDecimal bidPrice;


<h2>Spring JSON view Example</h2>
<a href="/rates">Currency Rates</a>

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


On clicking the 'Currency Rates' link:

Download JSON view as file

To download JSON as file, we need to set 'Content-Disposition' header in our controller method:

public String handleForexRequest(Model model, HttpServletResponse response) {
    response.setHeader("Content-Disposition", "attachment; filename=rates.json");
    model.addAttribute("currencyRates", getCurrencyRates());
    return "currencyRateView";

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 5.0.1.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.1.0 Java Servlet API
  • jackson-databind 2.9.2: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 1.8
  • Maven 3.3.9

Spring Atom Feed View Example Select All Download
  • json-view-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • webapp
            • WEB-INF
              • views

    See Also