Spring MappingJacksonJsonView, how to tell to use it instead of JSP view?

13,701

Solution 1

Spring will use Accept header sent by the client to return most appropriate view. Here you will find my complete Spring MVC application that returns both JSON and XML.

As you can see, I only needed:

<mvc:annotation-driven />

I also used the same annotations: @RequestMapping to map request to a method and @ResponseBody to tell Spring that what I am returning from the controller is the actual response. It might however need some tweaking/formatting, and here Spring takes care of marshalling your object into most appropriate type like JSON.

Solution 2

You should do it this way:

In your xml file set the following: set

<mvc:annotation-driven />

After it you need to set Jackson serializer:

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
  <list>
    <ref bean="jacksonMessageConverter"/>
  </list>
</property>
</bean>

after it you can use it in your Controller:

@RequestMapping(value="/getObjects",method = RequestMethod.POST)
     @ResponseBody
     public  List<MyObject> getCategories(){
     List<MyObject> objects = daoService.gettAllObjects();
     return objects;
    }
Share:
13,701
stivlo
Author by

stivlo

I'm a software developer, living in London. I'm interested in programming and system administration, in particular in the following areas: Java, JSP, JSTL, Ant, Maven TDD, JUnit, Mockito, Hibernate, JPA Spring Framework, Apache Lucene, iText PDF Amazon AWS: EC2, SimpleDB, SES, S3 Javascript, ExtJS, jQuery, Perl, PHP Linux, Postfix, Apache, Tomcat Currently learning Natural Language Processing with Gate My blog about programming &amp; sysadm &amp; travel: http://www.stefanolocati.it/ My github repos: https://github.com/stivlo Why I like stackoverflow: when I was in the University's Unix lab, I could ask questions to the gurus passing their days there and have wonderful inputs to solve practical problems and to improve my skills; being in stackoverflow, is a bit like hanging in the lab again. No, wait, actually it's much better, because of the wide skillset and quantity of geeks.

Updated on August 21, 2022

Comments

  • stivlo
    stivlo over 1 year

    I'm trying to use MappingJacksonJsonView with Spring 3.0, without success. I don't know what I'm doing wrong, I think the problem is that I don't know how to tell to use the MappingJacksonJsonView to render a request. I tried to use the same name for view name and bean name of MappingJacksonView, but didn't work. I built a sample test application here: https://github.com/stivlo/restjson

    In web.xml I've defined ContextLoaderListener and the mapping for dispatcherServlet.

    In servlet-context.xml I've added

    <mvc:annotation-driven/>
    

    and

    <bean name="jsonView"
        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
    

    In org.obliquid.restjson.web.ToDoList.java I set the logical view name as jsonView.

    However, instead of using MappingJacksonJsonView, it looks for a JSP file, according to my JSP mapping.

    message /restjson/WEB-INF/jsp/jsonView.jsp
    description The requested resource (/restjson/WEB-INF/jsp/jsonView.jsp) 
        is not available.
    

    What should I change to use MappingJacksonJsonView as a renderer?

    UPDATE 1: In following tests I've found that if I add the following to my servlet-context.xml, JSON rendering works, but my other view, rendered as JSP (home) is not working anymore.

    <!-- Resolve views based on string names -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    

    UPDATE 2: I removed the BeanNameViewResolver and changed my ToDoList.java to return directly the Collection to be converted in JSON, instead of ModelAndView, with a @ResponseBody annotation, as follows:

    @RequestMapping("/toDoList")
    public @ResponseBody List<ToDoItem> test() {
        List<ToDoItem> toDoList = new ArrayList<ToDoItem>();        
        toDoList.add(new ToDoItem(1, "First thing, first"));
        toDoList.add(new ToDoItem(1, "After that, do the second task"));
        return toDoList;
    }
    

    In this way it works. Even though the mapping is even more "magical". It makes me wonder, if a similar renderer exists for XML for instance, how does Spring know which renderer to pick?