我想用Spring 3.0 MVC来开发REST…我测试了一个非常简单的代码,但是org.springframework.beans.factory.BeanCreationException被抛出,这是我的代码:
@Controller
public class RestTest {
@RequestMapping(method=RequestMethod.GET, value="/1",
headers="Accept=application/json")
public @ResponseBody Employee getEmp(@PathVariable String id) {
Employee e = new Employee("helloWorld","stip of world");
return e;
}
}
beans-xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<context:component-scan base-package="org.stip.rest"></context:component-scan>
</beans>
例外:
org.springframework.beans.factory。BeanCreationException:创建名称为org.springframework.web.servlet.mvc.annotation的bean错误。在ServletContext资源[/WEB-INF/spring-servlet.xml]中定义的AnnotationMethodHandlerAdapter#0:当设置bean属性'messageConverters'与键[0]时,无法解析对bean 'jsonConverter'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/spring-servlet.xml]中定义名为'jsonConverter'的bean创建错误:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException: Could not instantiate bean类。:构造函数抛出异常;嵌套异常是java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.isFullyTyped()Z
jackson-core.jar和jackson-mapper已被添加到构建路径…我尽了最大的努力去修复……但是……
你不需要注册转换器,如果你添加jackson库到你的类路径spring将返回json到客户端每个@ResponseBody注释控制器方法。
还要检查是否:
"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
在applicationContext-web.xml顶部的schemaLocation标签中列出。