Results 1 to 10 of 10

Thread: Mapping Jackson Json View

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Default 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. #2
    Join Date
    Jul 2009
    Location
    Hyderabad India
    Posts
    56

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Default

    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?

  4. #4
    Join Date
    Jul 2009
    Location
    Hyderabad India
    Posts
    56

    Default

    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);
         });

  5. #5
    Join Date
    Jan 2011
    Posts
    3

    Unhappy

    Quote 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

  6. #6

    Default update

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

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

  7. #7
    Join Date
    Dec 2010
    Posts
    14

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

  8. #8
    Join Date
    Dec 2010
    Posts
    14

    Default

    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.

  9. #9

    Default

    Below is the indicative signature for the exception handler method

  10. #10
    Join Date
    Apr 2012
    Posts
    1

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •