【问题标题】:spring The request sent by the client was syntactically incorrect() issuespring 客户端发送的请求在语法上不正确()问题
【发布时间】:2015-04-17 10:25:07
【问题描述】:

我在主题行中提到了异常。您能否解释一下出了什么问题:

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

请帮我找出这个问题的原因。

谢谢, 沙迪普

【问题讨论】:

  • 您没有在 GET 请求正文 (it doesn't make much sense, anyway) 中发送任何内容,因此 Spring 无法通过 @RequestBody 绑定任何内容。您希望在控制器代码中的 body 值中看到什么?
  • 我想看看 RequestBody 是如何工作的。您的意思是 RequestBody 注释不适用于 GET?我会尝试 POST 方法。
  • 使用 POST 而不是 GET。现在您没有在 HTTP 请求正文中提供任何数据。
  • POST 工作正常
  • 请随意接受下面的答案:)

标签: spring model-view-controller


【解决方案1】:

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

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

否则定义方法为:

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

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多