Close

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).

Example

Additional Maven Dependency

pom.xml

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.2</version>
</dependency>

Java Config class

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {

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

  @Bean("currencyRateView")
  public View jsonView() {
      MappingJackson2JsonView view = new MappingJackson2JsonView();
      return view;
  }

  @Bean
  public ViewResolver jspViewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setPrefix("/WEB-INF/views/");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
  }
}

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

Spring Controller

@Controller
@RequestMapping("/")
public class MyController {

  @GetMapping("/")
  public String mainView() {
      return "main";
  }

  @GetMapping("/rates")
  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;
    .............
}

src/main/webapp/WEB-INF/views/main.jsp

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

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

mvn tomcat7:run-war

Output

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:

@GetMapping("/rates")
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
                • MyWebConfig.java
          • webapp
            • WEB-INF
              • views

    See Also