kittu
kittu

Reputation: 7008

Spring Framework's XmlBeanDefinitionStoreException: The prefix "beans" for element "beans:bean" is not bound

I am getting the following error when I pass request to 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.

Here is the 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();
    }
}

And 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 on the classpath:

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

I am not sure what is causing the problem. When I use new jars of jackson 2.7.4, I get another error:

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

Upvotes: 1

Views: 3534

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48123

The error is obvious and has nothing to do with Jackson:

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

It says the beans prefix or Namespace is not defined. Since the beans is the default namespace:

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

So you should remove the beans: prefix from:

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

The end result would be like:

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

If you're planning to enable JSON conversion by this piece of configuration, I should say there is no need for that. With the presence of the appropriate dependency on the classpath, Spring MVC would automatically register the required HttpMessageConverters for converting from/to JSON. So you can remove that configuration.

In the end, your dispatcher-servlet.xml would be like the following:

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

And a word of advice, if you're new to Spring Framework, it's better to start with Spring Boot.

Upvotes: 2

Related Questions