2015-04-17 3 views
1

Я получаю исключение, указанное в строке темы. Не могли бы вы объяснить, что это не так:spring Запрос, отправленный клиентом, был синтаксически неправильным() issue

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 

Просьба помочь мне выяснить причину этого вопрос.

Спасибо, Сандип

+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 –

Смежные вопросы