Announcement Announcement Module
Collapse
No announcement yet.
Mapping Jackson Json View Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mapping Jackson Json View

    Hi.

    I am new here. Can you please give me the code for a sample Spring MVC app using mappingjacksonjsonview to give JSON result to the view..

    Also would appreciate if you can provide me a jsp view to read the JSON output.

    Thanks

    Karthik

  • #2
    Check this -

    http://blog.springsource.com/2010/01...in-spring-3-0/

    Comment


    • #3
      Thanks Pranav.

      The app i am dev requires MappingJacksonJsonView to be used a as a bean to convert a model map into a JSON object... I read many articles which says this is possible, but nothing is clear on how...

      My code is smthing like this:

      A simformcontroller:

      return new ModelAndView("jsonView", modelMap);

      And:

      <bean name="jsonView" class="org.springframework.web.servlet.view.json.M appingJacksonJsonView">
      <property name="prefixJson" value="true"/>
      </bean>

      Is this supposed to work?? Or Am i missing something?

      Comment


      • #4
        No need of doing all these. Spring 3.0 has excellent support for AJAX / JSON. Use mvc:annotation-driven configuration and put Jackson jars in the classpath. Read the link that I sent earlier for more details.

        Sample code -

        Controller -
        Code:
        @RequestMapping("/getJSON")
        	public @ResponseBody
        	User getUser(@RequestParam String name) {
        		User user = new User();
        		user.setAge(30);
        		user.setName(name);
        
        		return user;
        	}
        servlet.xml -
        Code:
           <context:component-scan base-package="com.test.controller"/>
           <context:annotation-config />
        
           <mvc:annotation-driven />
        I am using jquery to make AJAX call and get the JSON object
        Code:
            $.getJSON("/sr/getJSON.action", { name: "Pranav" }, function(user) {
        		alert (user.name);
        		alert(user.age);
             });

        Comment


        • #5
          Originally posted by Pranav Kumar Varshney View Post
          No need of doing all these. Spring 3.0 has excellent support for AJAX / JSON. Use mvc:annotation-driven configuration and put Jackson jars in the classpath. Read the link that I sent earlier for more details.

          Sample code -

          Controller -
          Code:
          @RequestMapping("/getJSON")
          	public @ResponseBody
          	User getUser(@RequestParam String name) {
          		User user = new User();
          		user.setAge(30);
          		user.setName(name);
          
          		return user;
          	}
          servlet.xml -
          Code:
             <context:component-scan base-package="com.test.controller"/>
             <context:annotation-config />
          
             <mvc:annotation-driven />
          I am using jquery to make AJAX call and get the JSON object
          Code:
              $.getJSON("/sr/getJSON.action", { name: "Pranav" }, function(user) {
          		alert (user.name);
          		alert(user.age);
               });
          Does @ResponseBody also work for the methods annotated with @ExceptionHandler? At least for me this is not working. What I'm trying is, I'm writing RESTful/JSON service using Spring 3 MVC. For all happy scenarios, each operation returns me proper JSON response using @ResponseBody. But there will be some scenarios where there are some un-caught exceptions. To handle such scenarios, I've annotated a method with @ExceptionHandler annotation and returning a POJO which will have error code, error desc and annotated as @ResponseBody. But somehow this does not work. Any idea whats going wrong over here?

          Below is the indicative signature for the exception handler method...

          @ExceptionHandler( Exception.class )
          public @ResponseBody ErrorDetails handleUncaughtExceptions( Exception ex, HttpServletRequest req )



          Regards,
          - Jay

          Comment


          • #6
            update

            https://jira.springsource.org/browse/SPR-6902

            This is fixed in 3.1M1 - I just tested it.

            Comment


            • #7
              Facing HttpMediaTypeNotAcceptableException Could not find acceptable representation

              Hi,

              I am trying to send JSON response to my dojo widget. I have added <mvc:annotationdriven/> tag in my configuration file. Here is the code snippet which I am using:
              @RequestMapping("/getBrandJSON.htm")
              public @ResponseBody List<Brand> getBrand(){
              List<Brand> brandList = new ArrayList<Brand>();
              brandList = (List<Brand>) brandService.getBrandList();
              return brandList;
              }

              However, when I hit this using URL it result to an exception HttpMediaTypeNotAcceptableException: Could not find acceptable representation.

              The jackson-all-1.8.5.jar is added to my classpath.

              I am facing this issue and unable to resolve it.

              Please someone help.

              Regards,

              Bipul Sinha.

              Comment


              • #8
                Hi,

                The problem is almost resolved now. I have upgraded my jars from 3.0.0 to 3.1 and now I am able to view JSON response but unfortunately on the browser only. Just <mvc:anotationdriven/> and ackson-all jar works for me.

                However, the DOJO widget doesn't get populated with data .

                I believed that since it is because of Null ModelAndView returned to DispatcherServlet. Could someone confirm whether I am thinking on the right direction or not?

                Thanks in advance,

                Bipul Sinha.

                Comment


                • #9
                  Below is the indicative signature for the exception handler method

                  Comment


                  • #10
                    I tried this with Spring MVC 3.1 and Jackson 2.0 and it doesn't work. When I use the Jackson 1.9 jars it works. Any chance Jackson and Spring can work together to keep things in sync or do a better job of documenting exactly what versions works with which? Just saying "get jackson" isn't terribly helpful

                    Comment

                    Working...
                    X