Java spring @Controller 和 @RestController 注解的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25242321/
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-10 23:58:26  来源:igfitidea点击:

Difference between spring @Controller and @RestController annotation

javaspringspring-mvc

提问by Srikanth

Difference between spring @Controllerand @RestControllerannotation.

spring@Controller@RestController注解的区别。

Can @Controllerannotation be used for both Web MVC and REST applications?
If yes, how can we differentiate if it is Web MVC or REST application.

可以@Controller注解同时用于Web MVC框架和REST的应用程序?
如果是,我们如何区分是 Web MVC 还是 REST 应用程序。

采纳答案by micha

  • @Controlleris used to mark classes as Spring MVC Controller.
  • @RestControlleris a convenience annotation that does nothing more than adding the @Controllerand @ResponseBodyannotations (see: Javadoc)
  • @Controller用于将类标记为 Spring MVC 控制器。
  • @RestController是一个方便的注释,只不过是添加@Controller@ResponseBody注释(参见:Javadoc

So the following two controller definitions should do the same

所以以下两个控制器定义应该做同样的事情

@Controller
@ResponseBody
public class MyController { }

@RestController
public class MyRestController { }

回答by Bart

@RestControllerannotated classes are the same as @Controllerbut the @ResponseBodyon the handler methods are implied.

@RestController带注释的类与处理程序方法相同,@Controller@ResponseBody隐含了。

回答by CAAY

As you can see in Spring documentation (Spring RestController Documentation) Rest Controller annotation is the same as Controller annotation, but assuming that @ResponseBody is active by default, so all the json are parsed to java objects.

正如您在 Spring 文档(Spring RestController 文档)中看到的那样,Rest Controller 注释与 Controller 注释相同,但假设默认情况下 @ResponseBody 处于活动状态,因此所有 json 都被解析为 java 对象。

回答by sambhu

@RestControlleris composition of @Controllerand @ResponseBody, if we are not using the @ResponseBodyin Method signature then we need to use the @Restcontroller.

@RestController@Controllerand 的组合@ResponseBody,如果我们不使用@ResponseBodyin 方法签名,那么我们需要使用@Restcontroller.

回答by bertybro

Actually, be careful - they are not exactly the same.

实际上,要小心 - 它们并不完全相同。

If you define any interceptors within your application, they will not apply to Controllers annotated as @RestController, however they do work with @Controllerannotated controllers.

如果您在应用程序中定义了任何拦截器,它们将不适用于注释为 的控制器@RestController,但它们可以与带@Controller注释的控制器一起使用。

ie. configuration for the interceptor:

IE。拦截器的配置:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TemplateMappingInterceptor()).addPathPatterns("/**", "/admin-functions**").excludePathPatterns("/login**");
    }

}

and in the declaration of a Spring controller:

并在 Spring 控制器的声明中:

@Controller
public class AdminServiceController {...

Will work, however

会工作,但是

@RestController
public class AdminServiceController {...

does not end up having the interceptor being associated with it.

最终不会将拦截器与其关联。

回答by BERGUIGA Mohamed Amine

In the code below I'll show you the difference between @controller

在下面的代码中,我将向您展示两者之间的区别 @controller

@Controller
public class RestClassName{

  @RequestMapping(value={"/uri"})
  @ResponseBody
  public ObjectResponse functionRestName(){
      //...
      return instance
   }
}

and @RestController

@RestController

@RestController
public class RestClassName{

  @RequestMapping(value={"/uri"})
  public ObjectResponse functionRestName(){
      //...
      return instance
   }
}

the @ResponseBodyis activated by default. You don't need to add it above the function signature.

@ResponseBody默认情况下启用。您不需要将其添加到函数签名之上。

回答by yancy

THE new @RestController annotation in Spring4+, which marks the class as a controller where every method returns a domain object instead of a view. It's shorthand for @Controller and @ResponseBody rolled together.

Spring4+ 中新的 @RestController 注释,它将类标记为控制器,其中每个方法返回域对象而不是视图。这是@Controller 和@ResponseBody 组合在一起的简写。

回答by CoffeeBeanie

Instead of using @Controller and @ResponseBody, @RestController let's you expose Rest APIs in Spring 4.0 and above.

@RestController 让您在 Spring 4.0 及更高版本中公开 Rest API,而不是使用 @Controller 和 @ResponseBody。

回答by hi.nitish

@RestControllerwas provided since Spring 4.0.1. These controllersindicate that here @RequestMapping methods assume @ResponseBody semantics by default.

@RestController自 Spring 4.0.1 起提供。这些控制器表明这里 @RequestMapping 方法默认采用 @ResponseBody 语义。

In earlier versions the similar functionality could be achieved by using below:

在早期版本中,可以通过使用以下内容来实现类似的功能:

  1. @RequestMappingcoupled with @ResponseBodylike @RequestMapping(value = "/abc", method = RequestMethod.GET, produces ="application/xml") public @ResponseBody MyBean fetch(){ return new MyBean("hi") }

  2. <mvc:annotation-driven/>may be used as one of the ways for using JSON with Hymanson or xml.

  3. MyBean can be defined like
  1. @RequestMapping加上@ResponseBody喜欢@RequestMapping(value = "/abc", method = RequestMethod.GET, produces ="application/xml") public @ResponseBody MyBean fetch(){ return new MyBean("hi") }

  2. <mvc:annotation-driven/>可以用作将 JSON 与 Hymanson 或 xml 一起使用的方法之一。

  3. MyBean 可以像这样定义

@XmlRootElement(name = "MyBean") @XmlType(propOrder = {"field2", "field1"}) public class MyBean{ field1 field2 .. //getter, setter }

@XmlRootElement(name = "MyBean") @XmlType(propOrder = {"field2", "field1"}) public class MyBean{ field1 field2 .. //getter, setter }

  1. @ResponseBodyis treated as the view here among MVC and it is dispatched directly instead being dispatched from Dispatcher Servlet and the respective converters convert the response in the related format like text/html, application/xml, application/json .
  1. @ResponseBody在这里被视为 MVC 中的视图,直接调度而不是从 Dispatcher Servlet 调度,相应的转换器将响应转换为相关格式,如 text/html, application/xml, application/json 。

However, the Restcontroller is already coupled with ResponseBody and the respective converters. Secondly, here, since instead of converting the responsebody, it is automatically converted to http response.

然而,Restcontroller 已经与 ResponseBody 和相应的转换器耦合。其次,这里,由于不是转换响应体,而是自动转换为http响应。

回答by Ravi Wadje

If you use @RestControlleryou cannot return a view (By using Viewresolverin Spring/springboot) and yes @ResponseBodyis not needed in this case.

如果使用@RestController,则无法返回视图(Viewresolver在 Spring/springboot 中使用),并且@ResponseBody在这种情况下不需要yes 。

If you use @Controlleryou can return a view in Spring web MVC.

如果你使用@Controller你可以在 Spring web MVC 中返回一个视图。