SpringBoot @RestController與@Controller區別

@RestController

Spring4之後新加入的註解,原來返回JSON需要@ResponseBody和@Controller配合。

即@RestController是@ResponseBody和@Controller的組合註解

@RestController = @Controller + @ResponseBody;

@RestController
public class TestController {

    @RequestMapping(value="/hello", method= RequestMethod.GET)
    public String hello(){
        return "hello";
    }
}

與下面的一樣:

@Controller
@ResponseBody
public class TestController {

    @RequestMapping(value="/hello", method= RequestMethod.GET)
    public String hello(){
        return "hello";
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章