`

02 - Difference between spring @Controller and @RestController annotation

    博客分类:
  • S2SH
阅读更多

Difference between spring @Controller and @RestController annotation


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



Answer:

- @Controller is used to mark classes as Spring MVC Controller.

- @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations

引用

see: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html


org.springframework.web.bind.annotation
Annotation Type RestController


@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

A convenience annotation that is itself annotated with @Controller and @ResponseBody.
Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.


NOTE: @RestController is processed if an appropriate HandlerMapping-HandlerAdapter pair is configured such as the RequestMappingHandlerMapping-RequestMappingHandlerAdapter pair which are the default in the MVC Java config and the MVC namespace. In particular @RestController is not supported with the DefaultAnnotationHandlerMapping-AnnotationMethodHandlerAdapter pair both of which are also deprecated.

Since:
4.0
Author:
Rossen Stoyanchev, Sam Brannen



So the following two controller definitions should do the same:

@Controller
@ResponseBody
public class MyController { }


@RestController
public class MyRestController { }









-------------------------------------------------------------------

Spring 4.0 Restful 系列文章:


01 -Spring Framework: @RestController vs. @Controller
http://lixh1986.iteye.com/blog/2394351

02 - Difference between spring @Controller and @RestController annotation
http://lixh1986.iteye.com/blog/2394354

03 - SpringBoot: Building a RESTful Web Service
http://lixh1986.iteye.com/blog/2394363







- refere to :
difference-between-spring-controller-and-restcontroller-annotation
https://stackoverflow.com/questions/25242321









-
分享到:
评论

相关推荐

    Spring annotation

    Spring框架是Java开发中不可或缺的一部分,它通过提供丰富的注解简化了依赖注入、配置管理和AOP(面向切面编程)等任务。本文将深入探讨Spring注解及其在实际开发中的应用。 1. **依赖注入(Dependency Injection, ...

    jakarta.annotation-api-1.3.5-API文档-中文版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    point-cloud-annotation-tool

    github链接链接:https://github.com/springzfx/point-cloud-annotation-tool 在windows平台编译完成后的exe和dll文件,可以直接打开使用,并对原项目进行了优化,增加了列表和标注文件的显示功能,标注起来更快,...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    javax.annotation-api-1.3.2

    JDK9及以上版本没有javax.annotation-api-***.jar包 ,无法使用注解:@Resource JDK新特性,高版本JDK没有自带的javax(java扩展包)了。或者是使用的JDK不完整。 下载javax.annotation.jar包,导入到lib文件夹下,...

    jakarta.annotation-api-1.3.5-API文档-中英对照版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    SpringIoCAnnotation:Spring IoC @Annotation-有和没有@Autowired

    SpringIoCanotation Spring IoC @Annotation-有和没有@Autowired 实验!

    javax.annotation-api-1.3.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...

    spring 的Annotation方式

    ### Spring的Annotation方式详解 #### 引言 随着Spring框架的发展,其依赖注入(DI)机制也经历了从XML配置向注解驱动的重大转变。自Spring 3.0版本起,框架引入了一系列注解来简化依赖配置,使得开发人员能够在不...

    mybatis-plus-annotation-3.5.3.2.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.4.0.jar

    mybatis-plus-annotation-3.4.0.jar

    Spring注解 @Component、@Repository、@Service、@Controller区别

    ### Spring注解 @Component、@Repository、@Service、@Controller 的区别 #### 一、引言 在现代软件开发中,尤其是Java领域的企业级应用开发中,Spring框架因其灵活、强大的依赖注入(DI)和面向切面编程(AOP)...

    spring练习项目.7z

    资料包含spring-iocdi-annotation-document,iocdi-annotation-mvc,iocdi-xml-extend,iocdi-annotation-extend proxy,jdkproxy-transaction,jdkproxy-salary,day02-itheima11-spring-08-cglibproxy,day02-itheima11-...

    javax.annotation-api-1.3.2 jar包.rar

    `javax.annotation-api-1.3.2.jar` 包是Java标准版(Java SE)中的一个关键组件,它包含了用于开发和实现注解的API。这个API主要用于提供一些预定义的注解,这些注解可以增强代码的功能性和可维护性,同时减少对传统...

    androidx-annotation-1.0.0.jar

    androidx-annotation-1.0.0.jar

    spring 注解开发说明

    import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/welcome.htm") // 这个注解相当于实现Controller接口 public class WelcomeController { @RequestMapping ...

    mybatis-plus-annotation-3.2.0-API文档-中英对照版.zip

    赠送jar包:mybatis-plus-annotation-3.2.0.jar; 赠送原API文档:mybatis-plus-annotation-3.2.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.2.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...

    详解Spring_3.0基于Annotation的依赖注入实现

    ### 详解Spring 3.0基于Annotation的依赖注入实现 #### 概述 Spring框架作为一个广泛使用的Java开发框架,提供了强大的依赖注入(Dependency Injection, DI)能力,帮助开发者轻松管理和组织复杂的Java应用。随着...

    spring的annotation-driven配置事务管理器详解 (多数据源配置

    Spring 的 Annotation-Driven 配置事务管理器详解(多数据源配置) Spring 框架提供了强大的事务管理机制,通过使用 Annotation-Driven 配置,可以方便地管理事务。在多数据源配置中,spring 的 Annotation-Driven...

    Spring2.5 注解介绍(3.0通用)

    2. **使用`<context:annotation-config>`**: 这个命名空间指令会自动注册包括`AutowiredAnnotationBeanPostProcessor`在内的多个处理器,适用于常见的注解处理。 3. **使用`<context:component-scan>`**: 此指令...

Global site tag (gtag.js) - Google Analytics