gpt4 book ai didi

java - Spring框架的XmlBeanDefinitionStoreException : The prefix "beans" for element "beans:bean" is not bound

转载 作者:行者123 更新时间:2023-11-30 02:55:19 28 4
gpt4 key购买 nike

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

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 22 in XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 108; The prefix "beans" for element "beans:bean" is not bound.

org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 108; The prefix "beans" for element "beans:bean" is not bound.

这是 Controller :

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

类路径上的 jar :

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

我不确定是什么原因导致了这个问题。当我使用新的 jackson 2.7.4 jar 时,我收到另一个错误:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonTypeInfo$As

最佳答案

这个错误很明显,与 jackson 无关:

The prefix "beans" for element "beans:bean" is not bound.

它表示 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 框架的新手,最好从 Spring Boot 开始.

关于java - Spring框架的XmlBeanDefinitionStoreException : The prefix "beans" for element "beans:bean" is not bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37376868/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com