1
我收到主題行中提到的異常。能否請您解釋一下什麼是錯的:spring客戶端發送的請求在語法上不正確()問題
package mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/req")
public class Req {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String get(@RequestBody String body) {
return body;
}
}
用SpringMVC-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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--It tells the Spring framework to look for annotations and then do appropriate dependency injections.-->
<context:annotation-config/>
<!--used to provide base package to scan components-->
<context:component-scan base-package="mvc"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
當我試圖訪問http://localhost:8080/SpringMVC/req
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect().
Apache Tomcat/7.0.16
請幫我弄明白原因問題。
謝謝, 的Sandip
你不是在GET請求體發送任何([它並沒有多大意義,反正](http://stackoverflow.com/q/978061/1240557)),所以Spring不能通過'@ RequestBody'綁定任何東西。你期望在你的控制器代碼的'body'值中看到什麼? – kryger
我想檢查RequestBody的工作方式。 你的意思是RequestBody註解不適用於GET? 我會嘗試POST方法。 –
使用POST而不是GET。而現在,您並未在HTTP請求正文中提供任何數據。 – John