Spring MVC 和 JSON,未找到 Jackson 类异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9169517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC and JSON, Hymanson class not found exception
提问by jyriand
I cant get Spring's JSON support working. In my spring-servlet.xml file i have included following lines:
我无法获得 Spring 的 JSON 支持。在我的 spring-servlet.xml 文件中,我包含了以下几行:
<mvc:annotation-driven/>
<context:component-scan base-package="my.packagename.here" />
<context:annotation-config />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="HymansonMessageConverter"
class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"/>
I have also downloaded Hymanson libraries and added them to my eclipse project and also to WEB-INF/lib folder. When sending request to controller with jQuery getJSON method i get following errors:
我还下载了 Hymanson 库并将它们添加到我的 eclipse 项目和 WEB-INF/lib 文件夹中。当使用 jQuery getJSON 方法向控制器发送请求时,我收到以下错误:
javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
java.lang.NoClassDefFoundError: org/codehaus/Hymanson/JsonProcessingException
java.lang.ClassNotFoundException: org.codehaus.Hymanson.JsonProcessingException
What do you think is the problem. I'm guessing it has something to do with my spring-servlet.xmlfile. I can paste entire error log, if you need.
你觉得是什么问题。我猜这与我的spring-servlet.xml文件有关。如果需要,我可以粘贴整个错误日志。
采纳答案by gouki
JsonProcessingException is part of the Hymanson-core-asl-x.x.x.jar. Make sure that it's part of your classpath.
JsonProcessingException 是 Hymanson-core-asl-xxxjar 的一部分。确保它是您的类路径的一部分。
回答by Anshul Tiwari
For Hymanson v2 jars, class to be used for bean should be
对于 Hymanson v2 jar,用于 bean 的类应该是
<bean class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter" />
For older Hymanson version, org.springframework.http.converter.json.MappingHymansonHttpMessageConverter is ok. Make sure the jar files are added to the project library.
对于较旧的 Hymanson 版本, org.springframework.http.converter.json.MappingHymansonHttpMessageConverter 是可以的。确保将 jar 文件添加到项目库中。
回答by demongolem
The answer of anshul tiwari partially captures the problem. Here is a more complete answer ...
anshul tiwari 的回答部分解决了这个问题。这是一个更完整的答案......
When Hymanson made it to version 2.0, the core library got changed from Hymanson-core-asl-x.x.x.jar to Hymanson-core-x.x.x.jar. With that, the paths changed. In version 1, org.codehaus.Hymanson was the path. In version 2, it is in com.fasterxml.Hymanson.core if you were to open up the jar file.
当 Hymanson 到 2.0 版本时,核心库从 Hymanson-core-asl-xxxjar 更改为 Hymanson-core-xxxjar。随之,路径发生了变化。在版本 1 中,org.codehaus.Hymanson 是路径。在版本 2 中,如果您要打开 jar 文件,它位于 com.fasterxml.Hymanson.core 中。
Now if you have the libraries of version 2 and you are seeing the org.codehaus.Hymanson ClassNotFoundException, it means that there is a mixing of versions. Some code is expecting v1 but you have provided v2. This is certainly possible when using Spring so you have to be careful to choose the correct jar file for your code.
现在,如果您拥有版本 2 的库并且您看到 org.codehaus.Hymanson ClassNotFoundException,则意味着存在版本混合。某些代码需要 v1,但您提供了 v2。这在使用 Spring 时当然是可能的,因此您必须小心为您的代码选择正确的 jar 文件。
EDIT
编辑
In fact, looking at 3.2 Spring source code, org.springframework.http.converter.json.MappingHymansonHttpMessageConverterstill references the org.codehaus stuff so this is a case where Spring source code needs to import the correct path and there is nothing you the developer can do to use Hymanson 2.
实际上,查看 3.2 Spring 源代码,org.springframework.http.converter.json.MappingHymansonHttpMessageConverter仍然引用了 org.codehaus 的东西,所以这是 Spring 源代码需要导入正确路径的情况,开发人员无法使用 Hymanson 2 做任何事情。
回答by Tiago PC
Just to complement anshul tiwari answer, the bean tag should go inside mvc:annotation-driver:
只是为了补充 anshul tiwari 的回答,bean 标签应该放在 mvc:annotation-driver 中:
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<!-- <bean -->
<!-- class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"
/> -->
<bean
class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter" />
<!-- <bean -->
<!-- class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"
/> -->
</mvc:message-converters>
</mvc:annotation-driven>
回答by Anadi krishna
Use it Like below : Hope it will work..
像下面这样使用它:希望它会起作用..
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd" default-lazy-init="true">
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd“default-lazy-init="true">
<context:component-scan base-package="com.vc.bmp.resource" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
<!-- property name="prefixJson" value="true" />
<property name="supportedMediaTypes" value="application/json" /-->
<property name="objectMapper">
<bean class="com.fasterxml.Hymanson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.Hymanson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

