Can any one tell me how I can return string message from controller?
If i just return a string from a controller method then spring mvc treating it as a jsp view name.
Can any one tell me how I can return string message from controller?
If i just return a string from a controller method then spring mvc treating it as a jsp view name.
Although, @Tomasz is absolutely right there is another way:
but the first method is preferable. You can use this method if you want to return response with custom content type or return binary type (file, etc...);
For outputing
String
astext/plain
use:What about:
This woks for me.
This is just a note for those who might find this question later, but you don't have to pull in the response to change the content type. Here's an example below to do just that:
Annotate your method in controller with
@ResponseBody
:From: 15.3.2.6 Mapping the response body with the
@ResponseBody
annotation:With Spring 4, if your Controller is annotated with
@RestController
instead of@Controller
, you don't need the@ResponseBody
annotation.The code would be
You can find the Javadoc for
@RestController
here