日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Controller和RestController具體區(qū)別

在springboot中,Controller, RestController是使用控制器最常用的兩個(gè)注解,但是兩者之家的差異你知道嗎?本篇文章重點(diǎn)為大家講解一下Controller和RestController的區(qū)別。

1. Controller, RestController的共同點(diǎn)

都是用來(lái)表示Spring某個(gè)類(lèi)的是否可以接收HTTP請(qǐng)求。

2. Controller, RestController的不同點(diǎn)

@Controller:標(biāo)識(shí)一個(gè)Spring類(lèi)是Spring MVC controller處理器,@RestController:@RestController是@Controller和@ResponseBody的結(jié)合體,兩個(gè)標(biāo)注合并起來(lái)的作用。@Controller類(lèi)中的方法可以直接通過(guò)返回String跳轉(zhuǎn)到j(luò)sp、ftl、html等模版頁(yè)面。在方法上加@ResponseBody注解,也可以返回實(shí)體對(duì)象。@RestController類(lèi)中的所有方法只能返回String、Object、Json等實(shí)體對(duì)象,不能跳轉(zhuǎn)到模版頁(yè)面。

@RestController中的方法如果想跳轉(zhuǎn)頁(yè)面,則用ModelAndView進(jìn)行封裝,如下:

@RestController
public class UserController {

   @RequestMapping(value = "/index",method = RequestMethod.GET)
   public String toIndex(){
       ModelAndView mv = new ModelAndView("index");
      return mv;    
   }
}

示例如下:

@Controller  
@ResponseBody  
public class MyController { }  

@RestController  
public class MyRestController { }  

@Controller注解源碼:

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
   String value() default "";
}

@RestController注解源碼:

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
   String value() default "";
}

名稱欄目:Controller和RestController具體區(qū)別
本文路徑:http://www.5511xx.com/article/ccccdsc.html