Spring's Json not being resolved with appropriate response

前端 未结 8 2050
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 11:12

I\'ve tried to have a controller in Spring return a JSON response to no avail using the Jackson classes as recommended with 3.0. I\'ve got the jackson jar files(jackson-core

相关标签:
8条回答
  • 2020-12-03 12:11
    1. Make sure the POJO you return has get()ers, one for each field.
    2. Make sure the appserver (Tomcat) has the libraries even if you are sure your build system (Eclipse/Maven) does.

    I've had this error twice now. Just now I added getters to my pojo. The 406 error went away and I got JSON as expected. I assume that because my fields were package-protected (the default access), it would grab them, but I guess not. For the record, in case it matters, I also made the POJO implement Serializable, toString(), serialVersionUID, no-arg constructor, and explicit constructors.

    The prior time I cleaned/cleared/refreshed my Tomcat cache and did whatever else to force it to reload. I believe when I added the Jackson dependencies, it fixed my compile time errors, but since tomcat missed them, at runtime Spring MVC did not discover the Jackson libraries, and produced the 406 error about unacceptable response type.

    0 讨论(0)
  • 2020-12-03 12:13

    This question pops up everywhere on the net and I got bitten by it a couple of times. Spring 3.0.6 (and 5 possibly) has some issues in rendering json. Once I changed to 3.1.0.RELEASE version eveyrything worked AS IS. Without any config changes. Things to note is, the return method must have @ResponseBody (as in the example before) and must be in servlet-context.xml or your spring context configuration file.

    0 讨论(0)
提交回复
热议问题