2016-05-22 2 views
1

RestControllerにリクエストを渡すと、次のエラーが発生します。ここでSpring FrameworkのXmlBeanDefinitionStoreException:要素 "beans:bean"の接頭辞 "beans"がバインドされていません

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.

はコントローラです:

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

ジャー:

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

答えて

2

をエラーが明らかであるとジャクソンとは何の関係もありません:

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

それはbeansプレフィックスまたは名前空間ではないと言います定義された。 beansは、デフォルトの名前空間であるので:

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

ですからからbeans:プレフィックスを削除する必要があります。あなたが計画している場合

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
      <ref bean="jsonMessageConverter"/> 
     <list> 
    </property> 
</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> 

最終的な結果は以下のようになりますこの設定でJSON変換を有効にするには、と言うべきですが、その必要はありません。クラスパスに適切な依存関係が存在すると、Spring MVCは自動的にJSONとの変換に必要なHttpMessageConverterを登録します。 その設定を削除することができます。最後に

、あなたの 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 Bootで開始する方が良いでしょう。

+0

デフォルトの名前空間は、設定をより簡潔にしておく方がよいでしょう。 –

+0

jacksonの要求ハンドラマッピングが必要ですか?私はこれでかなり新しいです。接頭辞を削除すると、エラーが発生します。原因:org.springframework.beans.factory.NoSuchBeanDefinitionException: 'jsonMessageConverter'という名前のBeanが定義されていません。 – Satyadev

+0

classpthに 'jackson-databind'の依存関係がある場合は、それ。 –

関連する問題