spring客户端发送的请求在语法上是不正确的()问题

时间:2015-04-17 10:25:07

标签: spring model-view-controller

我在主题行中提到异常。你能解释一下是什么问题:

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

1 个答案:

答案 0 :(得分:0)

将方法更改为POST并使用HTTP POST发送一些数据,以便(@RequestBody String body)绑定起作用。

您可以使用SoapUi或HTTP Requester插件为浏览器方便地发送HTTP POST。

否则将方法定义为:

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
    public String get() {
        return "Some string";
    }