spring里面 @Controller和@RestController注解的区别

问题:spring里面 @Controller和@RestController注解的区别

spring里面 @Controller和@RestController注解的区别

Web MVC和REST applications都可以用@Controller吗?
如果是的话,怎么样区别这个一个 Web MVC还是REST application呢

回答一

下面的代码向你展示了@controller和 @RestController的区别


@Controller
public class RestClassName{@RequestMapping(value={"/uri"})@ResponseBodypublic ObjectResponse functionRestName(){//...return instance}
}
@RestController
public class RestClassName{@RequestMapping(value={"/uri"})public ObjectResponse functionRestName(){//...return instance}
}

@ResponseBody 在缺省的情况下会被激活. 你不需要在方法声明那里加上。

回答二

  • @Controller 用于标记类是 Spring MVC Controller
  • @RestController是一种加上了@Controller和@ResponseBody的便捷注解(请看Javadoc)
    所以下面定义的两个controller应该是一样的
@Controller
@ResponseBody
public class MyController { }@RestController
public class MyRestController { }

回答三

事实上,小心点,它们不是完全相同的

如果你的应用程序定义了任何interceptors,它们就不能作用在 注解了@RestController的Controllers 上,但是他们对于标注了@Controller的controller还是起作用的。

拦截器的配置:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new TemplateMappingInterceptor()).addPathPatterns("/**", "/admin-functions**").excludePathPatterns("/login**");}}

声明一个 Spring controller:

@Controller
public class AdminServiceController {...
Will work, however@RestController
public class AdminServiceController {...

最终就没有拦截器和它关联了

回答四

正如你在Spring文档(Spring RestController documentation)中看到的,RestController注解与Controller注解相同,但是@ResponseBody默认是生效的,因此所有Java对象在响应体中都被序列化为JSON格式。

回答五

Spring4以上版本中的新注解@RestController将类标记为一个controller,其中每个方法返回一个对象而不是一个视图。它是@Controller和@ResponseBody结合在一起的的缩写。

文章翻译自Stack Overflow:https://stackoverflow.com/questions/25242321/difference-between-spring-controller-and-restcontroller-annotation

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390919.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2流程控制

分支、循环 str1$1 str2$2 echo $# if [ $str1 $str2 ] thenecho "ab" elif [ "$str1" -lt "$str2" ] thenecho "a < b" elif [ "$str1" -gt "$str2" ] thenecho "a > b" elseecho "没有符…

客户行为模型 r语言建模_客户行为建模:汇总统计的问题

客户行为模型 r语言建模As a Data Scientist, I spend quite a bit of time thinking about Customer Lifetime Value (CLV) and how to model it. A strong CLV model is really a strong customer behavior model — the better you can predict next actions, the better yo…

linux bash命令_Ultimate Linux命令行指南-Full Bash教程

linux bash命令Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.欢迎使用我们的Linux命令行最终指南。 本教程将向您展示一些关键…

【知识科普】解读闪电/雷电网络,零基础秒懂!

知识科普&#xff0c;解读闪电/雷电网络&#xff0c;零基础秒懂&#xff01; 闪电网络的技术是革命性的&#xff0c;将实现即时0手续费的小金额支付。第一步是解决扩容问题&#xff0c;第二部就是解决共通性问题&#xff0c;利用原子交换协议和不同链条的状态通道结合&#xff…

spring框架里面applicationContext.xml 和spring-servlet.xml 的区别

问题&#xff1a;spring框架里面applicationContext.xml 和spring-servlet.xml 的区别 在Spring框架中applicationContext.xml和Spring -servlet.xml有任何关系吗? DispatcherServlet可以使用到在applicationContext.xml中声明的属性文件吗? 另外&#xff0c;为什么我需要*…

Alpha 冲刺 (5/10)

【Alpha go】Day 5&#xff01; Part 0 简要目录 Part 1 项目燃尽图Part 2 项目进展Part 3 站立式会议照片Part 4 Scrum 摘要Part 5 今日贡献Part 1 项目燃尽图 Part 2 项目进展 已分配任务进度博客检索功能&#xff1a;根据标签检索流程图 -> 实现 -> 测试近期比…

多维空间可视化_使用GeoPandas进行空间可视化

多维空间可视化Recently, I was working on a project where I was trying to build a model that could predict housing prices in King County, Washington — the area that surrounds Seattle. After looking at the features, I wanted a way to determine the houses’ …

蛮力写算法_蛮力算法解释

蛮力写算法Brute Force Algorithms are exactly what they sound like – straightforward methods of solving a problem that rely on sheer computing power and trying every possibility rather than advanced techniques to improve efficiency.蛮力算法听起来确实像是–…

NoClassDefFoundError和ClassNotFoundException之间有什么区别?是由什么导致的?

问题&#xff1a; NoClassDefFoundError和ClassNotFoundException之间有什么区别?是由什么导致的&#xff1f; NoClassDefFoundError和ClassNotFoundException之前的区别是什么? 是什么导致它们被抛出?这些问题我们要怎么样解决? 当我在为了引入新的jar包而修改现有代码…

关于Tensorflow安装opencv和pygame

1.安装opencv https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv C:\ProgramData\Anaconda3\Lib\site-packages>pip install opencv_python-3.3.1-cp36-cp36m-win_amd64.whlProcessing c:\programdata\anaconda3\lib\site-packages\opencv_python-3.3.1-cp36-cp36m-win_a…

内置的常用协议实现模版

SuperSocket内置的常用协议实现模版 中文&#xff08;中国&#xff09;Toggle Dropdownv1.6Toggle Dropdown关键字: TerminatorReceiveFilter, CountSpliterReceiveFilter, FixedSizeReceiveFilter, BeginEndMarkReceiveFilter, FixedHeaderReceiveFilter 阅读了前面一篇文档之…

机器学习 来源框架_机器学习的秘密来源:策展

机器学习 来源框架成功的机器学习/人工智能方法 (Methods for successful Machine learning / Artificial Intelligence) It’s widely stated that data is the new oil, and like oil, data needs the right refinement to evolve to be utilised perfectly. The power of ma…

linux gcc 示例_最好的Linux示例

linux gcc 示例Linux is a powerful operating system that powers most servers and most mobile devices. In this guide, we will show you examples of how to use some of its most powerful features. This involves using the Bash command line.Linux是功能强大的操作系…

帆软报表和jeecg的进一步整合--ajax给后台传递map类型的参数

下面是页面代码&#xff1a; <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8"%> <%include file"/context/mytags.jsp"%> <% String deptIds (String)request.getAttribute("…

@Nullable 注解的用法

问题&#xff1a;Nullable 注解的用法 我看到java中的一些方法声明为: void foo(Nullable Object obj){…}在这里Nullable是什么意思?这是不是意味着输入可以为空? 没有这个注解&#xff0c;输入仍然可以是null&#xff0c;所以我猜这不是它的用法? 回答一 它清楚地说明…

WebLogic调用WebService提示Failed to localize、Failed to create WsdlDefinitionFeature

在本地Tomcat环境下调用WebService正常&#xff0c;但是部署到WebLogic环境中&#xff0c;则提示警告&#xff1a;[Failed to localize] MEX0008.PARSING_MDATA_FAILURE<SOAP_1_2 ......警告&#xff1a;[Failed to localize] MEX0008.PARSING_MDATA_FAILURE<SOAP_1_1 ..…

呼吁开放外网_服装数据集:呼吁采取行动

呼吁开放外网Getting a dataset with images is not easy if you want to use it for a course or a book. Yes, there are many datasets with images, but few of them are suitable for commercial or educational use.如果您想将其用于课程或书籍&#xff0c;则获取带有图像…

git push命令_Git Push命令解释

git push命令The git push command allows you to send (or push) the commits from your local branch in your local Git repository to the remote repository.git push命令允许您将提交(或推送 )从本地Git存储库中的本地分支发送到远程存储库。 To be able to push to you…

在Java里面使用Pairs或者二元组

问题&#xff1a;在Java里面使用Pairs或者二元组 在Java里面&#xff0c;我的Hashtable要用到一个元组结构。在Java里面&#xff0c;我可以使用的什么数据结构呢&#xff1f; Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table ...回答一 我不认…

github 搜索技巧

1、关键词 指定开发语言 bitcoin language:javascript 2、关键词 stars 数量 forks 数量 bitcoin stars:>100 forks:>50