我正在使用Jboss 6.1,Spring 3.2,hibernate驗證以及最近啓用的CORS支持。請求正在使用GET方法正常工作。如果使用請求方法POST,我希望控制器返回405。但我得到的是http狀態碼400。我在jboss日誌中啓用了跟蹤,發現spring正在返回一個錯誤的處理程序。應用程序只有一個接受POST請求方法的控制器,剩下的控制器被定義爲只接受GET方法。每當使用POST請求方法爲這些控制器(使用GET定義)發出請求時,Spring都試圖找到一個處理程序,但它以某種方式將處理程序與POST方法相匹配。感謝你的幫助!Spring返回不正確的處理程序方法
web.xml中與CORS選項支持
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-context.xml</param-value>
</init-param>
<init-param>
<param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Spring上下文XML文件:
<mvc:annotation-driven validator="localValidatorFactoryBean">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper" />
</bean>
</mvc:message-converters>
這是我的捲曲腳本:
curl -i -X POST -H "Content-type:application/json" -d '{"param1":"value1","param2":"value2"}' http://localhost:8080/api/myrequest
捲曲響應:
HTTP/1.1 400錯誤的請求 服務器:Apache-狼/ 1.1 X-空間網絡服務的版本:2.0.0 的Content-Type:text/html的;字符集= UTF-8 的Content-Length :1079 日期:週一,二○一三年十一月一十八日23點十四分11秒GMT 連接:關閉
這是我的控制方法,它使用Hibernate豆validaton 1.1:
@RequestMapping(value = "myrequest", method = GET, produces = TEXT_XML_VALUE)
@ResponseBody
public String getDetails(
@Valid DetailRequest detailRequest, BindingResult results, HttpServletResponse resp) {
...
}