Spring Framework的XmlBeanDefinitionStoreException:前缀" beans" for element" beans:bean"不受约束

时间:2016-05-22 16:35:37

标签: java spring

当我将请求传递给RestController时,我收到以下错误。

  

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:   来自ServletContext资源的XML文档中的第22行   [/WEB-INF/mvc-dispatcher-servlet.xml]无效;嵌套异常是   org.xml.sax.SAXParseException; lineNumber:22; columnNumber:108;该   前缀"豆" for element" beans:bean"不受约束。

     

org.xml.sax.SAXParseException; lineNumber:22; columnNumber:108;该   前缀"豆" for element" beans:bean"不受约束。

这是控制器:

@RestController
public class smsController {

    @RequestMapping(value = "/sendSMS", method = RequestMethod.POST)
    public void sendMessage(@RequestBody MessageBean msgBean) throws UnsupportedEncodingException {

        String numbers = msgBean.getNumbers();    
        String message = msgBean.getMessages();
    }
}

dispatcher-servlet xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.spring.rest.controllers" />
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter"/>
            </beans:list>
        </beans:property>
    </beans:bean>
</beans>

类路径上的Jars:

jackson-annotations-2.3.2.jar
jackson-databind-2.3.2.jar
jackson-core-2.3.2.jar

我不确定导致问题的原因。当我使用jackson 2.7.4的新罐子时,我收到另一个错误:

  

java.lang.NoClassDefFoundError:   COM / fasterxml /杰克逊/注解/ JsonTypeInfo $为

1 个答案:

答案 0 :(得分:2)

错误很明显,与杰克逊无关:

  

前缀&#34; beans&#34; for element&#34; beans:bean&#34;不受约束。

它表示未定义beans前缀或命名空间。由于beans是默认命名空间:

xmlns="http://www.springframework.org/schema/beans"

因此,您应该从

中删除beans:前缀
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>

最终结果如下:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        <list>
    </property>
</bean>

如果您计划通过此配置启用JSON转换,我应该说不需要。由于存在对类路径的适当依赖性,Spring MVC将自动注册所需的HttpMessageConverter以便从/转换为JSON。 您可以删除该配置

最后,您的dispatcher-servlet.xml将如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.spring.rest.controllers" />
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

建议,如果您是Spring Framework的新手,最好从Spring Boot开始。