私はSpringMVC(Spring 3)とRESTを初めて使用し、小さなおもちゃのアプリを試してGETおよびPOSTWebサービスを試しています。Springの公式リファレンスを読み、Stackoverflow、RESTアプリケーションのRequestBody、Spring MVC 3でリクエストパラメーターを渡すでこれらの質問を見つけましたが、それでも機能させることができません。誰かが私が逃したことについてのヒントを私に与えることができますか?
私のコントローラーは次のようなものです。
@Controller
@RequestMapping(value = "/echo")
public class EchoControllerImpl implements EchoController {
@RequestMapping(method = RequestMethod.POST, value = "/echoByPost")
public ModelAndView echoByPost(@ModelAttribute EchoDto input) {
// ...
}
}
対応するコンバーターをアプリctxに配置しました:
<mvc:annotation-driven />
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>foo.EchoDto</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingHttpMessageConverter" />
<ref bean="stringHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
<!-- other beans like ViewResolvers -->
どこかでそれについて言及しているのを見たので、これらをweb.xmlに追加しようとしました(それが何を意味するのかはよくわかりませんが)
<filter>
<filter-name>httpPutFormFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormFilter</filter-name>
<servlet-name>spring-rest</servlet-name>
</filter-mapping>
次に、CurlでWebサービスを呼び出そうとしました。
curl -d "echoDto=<EchoDto><message>adrian</message></EchoDto>" http://localhost:8001/foo/rest/echo/echoByPost.xml
着信オブジェクトは、JAXB2のアンマーシャリングによって作成されていないことがわかりました。代わりに、リクエストxmlメッセージ全体を含むctor EchoDto(String)が呼び出されているようです。
(代わりに@RequestBodyでパラメーターに注釈を付けようとしましたが、さらに悪いことに、コントローラーメソッドを呼び出すことさえできません)
誰かが私が逃したことを教えてもらえますか?
Jaxb2MarshallerはDTOクラスで正しくセットアップされています。これは、別のGETRESTWebサービス呼び出しの場合に返されるモデルオブジェクトとして使用できます。