2015-04-17 120 views
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

+0

你不是在GET请求体发送任何([它并没有多大意义,反正](http://stackoverflow.com/q/978061/1240557)),所以Spring不能通过'@ RequestBody'绑定任何东西。你期望在你的控制器代码的'body'值中看到什么? – kryger

+0

我想检查RequestBody的工作方式。 你的意思是RequestBody注解不适用于GET? 我会尝试POST方法。 –

+0

使用POST而不是GET。而现在,您并未在HTTP请求正文中提供任何数据。 – John

回答

0

更改方法为POST并用HTTP POST所以(@RequestBody字符串体)结合作品发送一些数据。

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

否则定义方法:

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

工作罚款POST –

相关问题