As in the question, Spring 2.5 is used; @ResponseBody
was introduced in Spring 3.0
@see ResponseBody JavaDocs in Latest Spring
Back to the question.
In my Spring controller how can i return a string ...
The simple answer as per Spring Blog Post you need to add OutputStream
as a parameter to your controller method.
You XML based configuration should look like this
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="your.controllers"/>
</beans>
And your controller should look like this
package your.controllers;
@Controller
public class YourController {
@RequestMapping(value = "/yourController")
protected void handleRequest(OutputStream outputStream) throws IOException {
outputStream.write(
"YOUR DESIRED STRING VALUE".getBytes()
);
}
}
Finally if you want to do it without any kind of annotations at all, then;
Your XML based configuration should look like this
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/yourController" class="your.controllers.YourController"/>
</beans>
And your controller should look like this
package your.controllers;
public class YourController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getOutputStream().write(("YOUR DESIRED STRING VALUE").getBytes());
return null;
}
}