java - 仅从 Spring MVC 3 Controller 返回字符串消息

标签 java spring spring-mvc

谁能告诉我如何从 Controller 返回字符串消息?

如果我只是从 Controller 方法返回一个字符串,那么 spring mvc 将其视为 jsp View 名称。

最佳答案

使用 @ResponseBody 在 Controller 中注释您的方法:

@RequestMapping(value="/controller", method=GET)
@ResponseBody
public String foo() {
    return "Response!";
}

发件人:15.3.2.6 Mapping the response body with the @ResponseBody annotation :

The @ResponseBody annotation [...] can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).

关于java - 仅从 Spring MVC 3 Controller 返回字符串消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7672858/

相关文章:

java - 如何在 Java 中为 REST 服务获取 WSDL

java - 获取 java.lang.IllegalArgumentException : argument type mismatch error on using "RedirectAttributes" in spring MVC

java - 混合 Spring MVC + Spring Data Rest 导致奇怪的 MVC 响应

xml - Spring : how to expose SimpMessagingTemplate bean to root context ?

java - 如何使用Java将HTML页面转换为PDF文档?

java - 针对外部 API 的 Spring 身份验证

java - 如何使用 contains 从 Arraylist 中删除对象

java - spring data redis 无法连接Redis DNS代理

spring - GridFSDBFile 无法转换为 org.springframework.web.multipart.MultipartFile

java - 文件没有从 Apache Web 服务器传输到 Tomcat 应用程序服务器