1

I am getting exception mentioned in subject line. Could you please explain what is wrong:

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>

When i tries to access 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

Kindly help me in figure out the cause of this issue.

Thanks, Sandip

  • You're not sending anything in the GET request body ([it doesn't make much sense, anyway](http://stackoverflow.com/q/978061/1240557)), so Spring can't bind anything via `@RequestBody`. What did you expect to see in the `body` value in your controller code? – kryger Apr 17 '15 at 11:24
  • I want to check how RequestBody works. You mean RequestBody annotation won't work for GET? I will try for POST method. – Sandip Gaikwad Apr 17 '15 at 11:38
  • use POST instead of GET. And right now you are not providing any data in HTTP request body. – John Apr 17 '15 at 11:41
  • For POST it worked fine – Sandip Gaikwad Apr 17 '15 at 12:16
  • feel free to accept the answer bellow then :) – John Apr 17 '15 at 12:39

1 Answers1

0

Change method to POST and send some data with HTTP POST so the (@RequestBody String body) binding works.

You could use SoapUi or HTTP Requester plugin for browsers to conveniently send HTTP POST.

Otherwise define method as:

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
    public String get() {
        return "Some string";
    }
John
  • 4,608
  • 2
  • 32
  • 60