日期:2014-05-16  浏览次数:20433 次

spring3 mvc json问题
SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
java.lang.NoSuchMethodError: org.codehaus.jackson.map.type.TypeFactory.type(Ljava/lang/reflect/Type;)Lorg/codehaus/jackson/type/JavaType;
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.getJavaType(MappingJacksonHttpMessageConverter.java:114)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.canRead(MappingJacksonHttpMessageConverter.java:91)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:618)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:587)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:336)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:169)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)



这问题的解决方法是:

加入jackson包,四个jar:
jackson-core-lgpl-1.7.5.jar
jackson-core-asl-1.7.5.jar
jackson-mapper-asl-1.7.5.jar
jackson-mapper-lgpl-1.7.5.jar

在spring mvc 的配置文件加上
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

在controller中你的方法如下注解:
@RequestMapping(value = "/getBzrByOrganizationId/{id}", method = RequestMethod.POST)
@ResponseBody
public Teacher login(@PathVariable Long id){.......return new Teacher();}

好了,你可以开始Ajax的json传递了。。。。

记得jackson 包要1.3+的,低版本的依然会报错的