Java Spring MVC 控制器方法的有效返回类型是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29365833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:51:49  来源:igfitidea点击:

What are valid return types of a Spring MVC controller method?

javaspringspring-mvcmodel-view-controllerannotations

提问by Java Surfer

I have not much experience in Spring MVC and I have the following about what are the valids return types that a controller method can return.

我在 Spring MVC 方面没有太多经验,我有以下关于控制器方法可以返回的有效返回类型的信息

So I know that the user generate an HttpRequestreceived and handled by the DispatcherServletthat dispatch this request to a specific controller class.

所以我知道用户生成一个由DispatcherServlet接收和处理的HttpRequest,该请求将该请求分派到特定的控制器类。

A controller class is something like this:

控制器类是这样的:

@Controller
public class AccountController {

    @RequestMapping("/showAccount")
    public String show(@RequestParam("entityId") long id, Model model) {
        ...
    }

    .......................................
    .......................................
    .......................................
}

So I know that each method handle a specific request and that the handled request is specified by the @RequestMappingannotation.

所以我知道每个方法处理一个特定的请求,并且处理的请求是由@RequestMapping注释指定的。

I also know that the method return a Stringobject that is the logical view name(that then is resolved by the view resolverto render the view)

我也知道该方法返回一个String对象,它是逻辑视图名称(然后由视图解析器解析以呈现视图)

So, at this stage, I think that a method of a controller class returns only Stringobject. But I am not sure of it. Maybe a method like this can return also some different kind of objects?

所以,在这个阶段,我认为控制器类的方法只返回String对象。但我不确定。也许这样的方法也可以返回一些不同类型的对象?

采纳答案by Rajeev

There are many return types are available for Handler method which is annotated by @RequestMappinginside the controller, like :

Handler 方法有许多返回类型可用,它们@RequestMapping在控制器内部进行了注释,例如:

  • ModelAndView (Class)

  • Model (Interface)

  • Map
  • String
  • void
  • View
  • HttpEntity<?>or ResponseEntity<?>
  • HttpHeaders
  • 模型和视图(类)

  • 型号(接口)

  • 地图
  • 细绳
  • 空白
  • 看法
  • HttpEntity<?>或者 ResponseEntity<?>
  • HttpHeaders

and much more.....See Docs

还有更多.....见文档

Every return type have its specific use for example: If you are using String then it means return View Name and this view name will resolved by ViewResolver. If you don't want to return any view name mention return type as void. If you want to set view name as well as want to send some data to view use ModelAndViewas a return type.

每个返回类型都有其特定用途,例如:如果您使用的是 String 那么这意味着返回视图名称并且此视图名称将由ViewResolver. 如果您不想返回任何视图名称,请提及返回类型为void. 如果您想设置视图名称以及想发送一些数据来查看ModelAndView用作返回类型。

Please go through the documentation you will also get to know what kind of method argument you can pass in the handler method.

请仔细阅读文档,您还将了解可以在处理程序方法中传递什么样的方法参数。

回答by Master Slave

You have a direct answer in the doc

你在文档中有一个直接的答案

Take a special note of the

请特别注意

If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters.

如果该方法使用@ResponseBody 进行注释,则返回类型将写入响应 HTTP 正文。返回值将使用 HttpMessageConverters 转换为声明的方法参数类型。

When the method is annoated with @ResponseBody, the return type can be any custom type, any Java pojo, that the framework will convert to an appropriate repsentation JSON, XML or the like and write back to the response body

当方法用@ResponseBody 注释时,返回类型可以是任何自定义类型,任何 Java pojo,框架将转换为适当的表示 JSON、XML 等并写回响应正文

回答by marco.eig

you could take a look at AnnotationMethodHandlerAdapter#getModelAndView. This method has several conditions based on the return type to choose what to do with the returned value.

你可以看看AnnotationMethodHandlerAdapter#getModelAndView。这个方法有几个基于返回类型的条件来选择如何处理返回值。