简体   繁体   中英

Spring MVC REST: org.springframework.beans.factory.BeanCreationException was thrown

I want to use Spring 3.0 MVC to develop REST...I test a very simple code ,but org.springframework.beans.factory.BeanCreationException was thrown here is my code:

@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>

the exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'jsonConverter' while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonConverter' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.isFullyTyped()Z

jackson-core.jar and jackson-mapper have been added to the build path... i try my best to fix..but..Can you help me ...Thanks

Also make sure to check if:

"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

are listed in the schemas at the top of your applicationContext-web.xml in the schemaLocation tag.

如果您将jackson库添加到类路径中,则不需要寄存器转换器。spring将为每个@ResponseBody注释的控制器方法将json返回给客户端。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM