自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java Spring 框架 @Controller 與 @RestController的區(qū)別,你明白了嗎?

開發(fā) 前端
可以使用@Controller注釋和@ResponseBody注釋來完成@RestController的工作,RESTful Web 服務(wù)的默認(rèn)的。Spring 引入了@RestController ,它結(jié)合了@Controller和@ResponseBody的功能。

概述

@Controller注解長期用于傳統(tǒng)的spring MVC 框架Controller。@ RestController注解是在Spring 4.0 中引入的,以簡化RESTful Web 服務(wù)的創(chuàng)建。

Spring MVC/Spring BOOT中的@RestController注解其實(shí)就是@Controller和@ResponseBody注解的組合。

它被添加到Spring 4.0中,使得在Spring框架中開發(fā)RESTful Web服務(wù)更加容易。

如果對(duì)**REST Web 服務(wù)有所了解的小伙伴,**那么對(duì) REST API 和 Web 應(yīng)用程序之間的區(qū)別應(yīng)該不陌生。

這是來自Web 應(yīng)用程序的響應(yīng)通常是視圖(HTML + CSS),因?yàn)樗鼈兪菫槿祟愑^眾準(zhǔn)備的。

REST API只是以 JSON 或 XML 的形式返回?cái)?shù)據(jù),大多數(shù) REST 客戶端都是程序。這種區(qū)別在@Controller和**@RestController**注解中也很明顯。

Spring MVC/BOOT中@Controller和@RestController的區(qū)別

  1. @Controller是在 Spring 中將類標(biāo)記為控制器類的注解,而@RestController用于REST Web 服務(wù),類似于@Controller和@ResponseBody。
  2. @Controller注釋表示該類是控制器,如 Web 控制器,而@RestController注釋表示該類是控制器,其中@RequestMapping方法默認(rèn)采用@ResponseBody(即 REST API)。
  3. 關(guān)鍵區(qū)別在于,一旦用@RestController注釋類,就不需要在每個(gè)處理程序方法上都使用**@ResponseBody**。
  4. @Controller創(chuàng)建模型對(duì)象的映射并查找視圖,而**@RestController**只是簡單地返回對(duì)象和對(duì)象數(shù)據(jù),直接以 JSON 或 XML 形式寫入 http 響應(yīng)。

可以使用@Controller注釋和@ResponseBody注釋來完成@RestController的工作,RESTful Web 服務(wù)的默認(rèn)的。Spring 引入了@RestController ,它結(jié)合了@Controller和@ResponseBody的功能。

例子

以下是 Spring 中相等的示例代碼。

  • @ Controller
@Controller
@ResponseBody
Public class MVCController{
}
  • @ RestController
@RestController
public class MVCController{
}

以上兩段代碼在Spring MVC/Spring Boot中是同樣的操作。

結(jié)論

以上就是Spring MVC/Spring Boot的@Controller注解和@RestController注解的區(qū)別。

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2024-02-04 00:00:00

@ValidSpring@Validated

2023-10-27 07:39:44

IOC容器Spring

2022-10-24 20:25:40

云原生SpringJava

2024-01-25 09:10:10

GoRust標(biāo)準(zhǔn)庫

2022-09-27 07:31:57

Property模式數(shù)據(jù)

2024-10-28 08:26:55

SpringAI語言模型1. 多模型

2022-04-07 11:15:22

PulseEventAPI函數(shù)

2022-12-30 08:35:00

2023-05-31 08:29:08

數(shù)據(jù)庫CPU類型

2023-04-26 00:00:00

框架Vue.js客戶

2022-04-07 08:20:22

typeinterface前端

2022-10-08 00:24:40

嵌套事務(wù)加入事務(wù)事務(wù)

2022-10-10 18:38:56

inert屬性鍵盤

2023-12-08 08:38:15

EventLoopAPI瀏覽器

2023-12-28 08:43:28

前端算法搜索

2022-10-19 08:19:32

動(dòng)態(tài)基線預(yù)警

2024-01-08 20:05:32

2023-11-06 07:37:01

函數(shù)式插槽React

2023-06-09 07:18:03

開源數(shù)據(jù)庫

2022-05-31 07:32:19

JDK8API工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)