java - 为什么我需要将 @ResponseBody 添加到我的 Controller 操作中?

标签 java spring spring-mvc annotations

根据Spring documentation ,此注释指示方法返回值应绑定(bind)到 Web 响应正文。我明白这一点,并且我一直在使用它来进行 ajax 调用。但是,我最近遇到了不使用注释的代码。

所以我想我的问题确实是为什么它在没有注释的情况下也能工作?

最佳答案

如果没有注释,则会发生不同的过程。根据返回类型 ( you can find the defaults in this document ),将生成不同的响应。

例如,如果您的返回类型是 String ,那么默认情况下,返回值将解析为 View名字,a ViewResolver将尝试解决并创建 View对象,以及 RequestDispatcher将转发/包含/重定向到它(例如 jsp ),以便 Servlet容器可以处理生成响应。

处理返回类型的实际接口(interface)是HandlerMethodReturnValueHandler并且每种类型都有许多实现。 See here for more information .

关于java - 为什么我需要将 @ResponseBody 添加到我的 Controller 操作中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281522/

相关文章:

java - Java 的 AES 加速

Java:在写入之前检查对象是否存在于对象文件中

java - 无法在 spring boot 应用程序中 Autowiring 接口(interface)

java - Java 中的 PropertyVetoException

java - 如何在 Spring MVC 中将请求映射到 HTML 文件?

java - Spring MVC+ hibernate : createSQLQuery is not valid without active transaction

Java7 : What is some equivalent to specifing method references in a function call

java - 为大数据生成最佳 UUID

java - 使用带有 JDBC ignite 的 Spring Boot Web 应用程序而不是连接 ignite 的 dot NET native 方法对性能有影响吗?

spring-mvc - 结合mvc :interceptors & mvc:resources to cache only static resources