Spring's Json not being resolved with appropriate response

前端 未结 8 2051
佛祖请我去吃肉
佛祖请我去吃肉 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.

提交回复
热议问题