2

I have a Spring MVC project with Maven managing dependencies. I need to read the JSON and display its content to the view.

Given a simple JSON object

{
    "items" : [{"model" : "m1"}, {"model" : "m2"}, {"model" : "m3"}]
}

I leverage packages from Jackson Project to read and parse the file, and then set the value in @Controller

JsonNode itemsNode = Node.path("items");
model.addAttribute("items", itemsNode);

On the JSP, I retrieve the values

Item 0: ${items.get(0)}, Item 1: ${items.get(1)}, Item 2: ${items.get(2)}

The problem I encountered is,

everything works as expected when I use

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

but I got error,

HTTP Status 500 - javax.el.MethodNotFoundException: Unable to find unambiguous method: class com.fasterxml.jackson.databind.node.ArrayNode.get(java.lang.Long)

when I replaced both <dependency> to

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

with no source code changes(except import statements). Spring is 4.1.5.RELEASE

Dino Tw
  • 3,167
  • 4
  • 34
  • 48

1 Answers1

2
${items.get(0)}

The JSP is treating the 0 as a Long, but ArrayNode.get() takes an int. Check out the answer to this question for more details. In short, you could try this:

${items.get( (0).intValue() )}
Community
  • 1
  • 1
John R
  • 2,066
  • 1
  • 11
  • 17
  • Yes, I noticed that. So tried `Integer.parseInt("0")` and `new Integer(0)` with no luck. And your approach works, thank you very much. But I also wonder why 0 is treated differently on different version of Jackson. – Dino Tw Mar 03 '15 at 20:52
  • Yea I was wondering that myself. I took a look at the Javadocs and both versions had get(int). – John R Mar 03 '15 at 21:14
  • I was assuming it should be JEE implementation's job to interpret JSP, and found that Tomcat 8.0.9 gave me another error `Unable to find unambiguous method: class com.fasterxml.jackson.databind.node.ArrayNode.get(java.lang.Integer)`. But the later version of Tomcat works fine with your approach(verified on 8.0.12 and 8.0.20). – Dino Tw Mar 03 '15 at 23:16
  • Make sure to use a recent version of Jackson: some problems with `int` vs `Integer` overloading were resolved in 2.2 or such; so version like 2.5.1 should work better. – StaxMan Mar 04 '15 at 18:17