文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html

@RestController注解相当于@ResponseBody + @Controller合在一起的作用

//  一般用于接口   或   前后端分离

1.如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp,html页面,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是return 里的内容。

// 一般用于后台页面

2.如果需要返回到指定页面,则需要用   @Controller  配合视图解析器   InternalResourceViewResolver   才行。

如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

例如

1.如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp,html页面,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是return 里的内容。

package com.web.TestRestController;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody; @RestController
@RequestMapping("/TestController")
public class TestRestController{ // 返回 return 里面的内容
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(){
// 返回 return 里面的内容 如字符串 json xml 或自定义返回
return "{}";
}
}

  

2.如果需要返回到指定页面,则需要用   @Controller  配合视图解析器   InternalResourceViewResolver   才行。

如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

package com.web.TestController;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/TestController")
public class TestController{ // 映射文件
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(){
// 他就会映射到 TestController 目录下 index.jsp 或 index.html 文件
return "TestController/index";
} // 返回 return 里面的内容
@RequestMapping(value = "index", method = RequestMethod.GET)
@ResponseBody
public String index(){
// 返回 return 里面的内容 如字符串 json xml 或自定义返回
return "{}";
}
}

文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html

@RestController 与 @Controller 注解区别的更多相关文章

  1. 注解@RestController与@Controller的区别

    开发RESTful API 时,一般都会在Controller上加上@Controller注解,但是有时候加上@RestController,当同事问为什么的时候,我也一脸懵逼,默默的看了资料,现在就 ...

  2. @RestController和@Controller注解的区别

    @RestController是@ResponseBody和@Controller注解的结合,当你return一个页面时,使用@Controller注解返回的是相应页面,使用@RestControll ...

  3. RestController 和Controller的区别

    restful风格,restcontroller与controller 初步接触springmvc的时候,被要求使用restful风格,彼时一头雾水,不懂何谓restful,参阅了很多资料,慢慢的接触 ...

  4. @restcontroller与@controller的区别

    这段时间偷偷看了下spring boot.结果引用模板时没注意,把@restcontroller替换了@controlle,结果模板出不来.终究原因是spring的知识不到位. 下面说说这2的说明和区 ...

  5. 浅谈@RestController和@Controller的区别

    在做Spring MVC开发时,如果对@RestController或者@Controller这两个注解理解不够清晰的话,就难免会出现用混的情况.而混用的结果往往是无法实现期望的跳转结果或者是直接将跳 ...

  6. SpringBoot 中 @RestController 和 @Controller 的区别

    1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...

  7. RestController和Controller的区别

    知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用. 1) 如果只是使用@RestController注解Controller,则Co ...

  8. SpringMVC中@RestController和@Controller的区别

    在使用SpringMVC时,对于Controller中的注解@RestController和@Controller需要我们区分清楚 @RestController注解相当于@ResponseBody和 ...

  9. @Component注解、@Service注解、@Repository注解、@Controller注解区别

    --------------------------------------------------------------------------------------------------- ...

随机推荐

  1. Luogu P1156 垃圾陷阱 【dp】By cellur925

    题目传送门 这题...看上去浓浓的背包气息...但是并不好设计状态啊emmm. 我们考虑可能成为状态的量:高度.血量.时间.物品.看数据范围也猜到应该大概是个二维dp了w. 正确的状态设计之一:设$f ...

  2. jsp include

    1.<%@ include file="a.jsp"%> 路径无法动态赋值,只能写成固定路径: 生成一个jsp页面,整个编译 2.<jsp:include pag ...

  3. c++,类的对象作为形参时一定会调用复制构造函数吗?

    c++,类的对象作为形参时一定会调用复制构造函数吗? 答:如果参数是引用传递,则不会调用任何构造函数:如果是按值传递,则调用复制构造函数,按参数的值构造一个临时对象,这个临时对象仅仅在函数执行是存在, ...

  4. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D

    Description A tree is an undirected connected graph without cycles. The distance between two vertice ...

  5. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A

    Description Bear Limak wants to become the largest of bears, or at least to become larger than his b ...

  6. 简单几何(求交点) UVA 11178 Morley's Theorem

    题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...

  7. Finally语句

  8. background-origin与background-clip的“区别”

    css3新增了一些背景相关的属性,其中background-origin与background-clip是比较让人困惑的: background-origin:用于指定绘制背景图片的起点.默认值:pa ...

  9. 如何正确从他人机器MySQL数据库下拷贝出.sql,再导入到自己windows下MySQL数据库(图文详解)

    不多说,直接上干货! 我这里,是放在桌面上. 登陆数据库 然后, mysql -uroot -p 默认是回车. 创建数据库 CREATE DATABASE securityonion_db; 目的,就 ...

  10. java之idea打jar包

    1.选中项目右键,选择Open module settings,如图: 2.点击Artifacts,添加jar,如图: 3.create jar,必须选择项目的更目录 4.build artfacts ...