コントローラからJSON/XMLデータを返す必要があります。見つけたものから@ResponseBody
、メソッドに必要であり、そのために<mvc:annotation-driven>
有効にする必要があります。私はあらゆる種類のRnDを試しましたが、まだ行き詰まっています!:(
どうやら私の問題は私のservlet.xmlファイルにあります(スキーマは検証されていません!)私はSpring 3.1.1を使用していて、クラスパスにspring-mvc-3.1.1.jarを明示的に入れています。
これが私のサーブレットコンテキストファイルsample-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc-3.1 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sample.controller"/>
<mvc:annotation-driven/>
<!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.sample.model.SampleClass</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
私のコントローラークラスは次のようになります:
@Controller
public class XmlController {
@RequestMapping(value="/getXml",method = RequestMethod.POST)
public @ResponseBody AssociateDetail getXml(){
System.out.println("inside xml controller.....");
AssociateDetail assoBean=null;
try{
AssociateService add=new AssociateService();
assoBean=add.selectAssociateBean();
}catch(Exception e){
e.printStackTrace();
}
return assoBean;
}
}
今問題は<mvc:annotation-driven />
エラーを与えることです:
cvc-complex-type.2.4.c:一致するワイルドカードは厳密ですが、要素'mvc:annotation-driven'の宣言が見つかりません。
そして、私はこのサイト以降で提案されているすべての回避策を試しました。Spring 3.1.1およびを使用して、スキーマの名前空間を更新しました@ResponseBody
。