@RestController和@Controller區別

@RestController = @Controller + @ResponseBody

使用@Controller 註解,在對應的方法上,視圖解析器可以解析return 的jsp,html頁面,並且跳轉到相應頁面

若返回json等內容到頁面,則需要加@ResponseBody註解

即 若加的是@ResponseBody註解,則不能返回到html等頁面,返回的是json內容到頁面

@Controller
@EnableAutoConfiguration
public class HelloController {
    @RequestMapping("/test")
    public String test(){
        return "index";
    }
    public static void main(String[] args) {
        SpringApplication.run(HelloController.class);
    }
}

 index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
</head>
<body>
 <p>我是INDEX</p>
</body>
</html>

application.yml 

spring:
  thymeleaf:
    prefix: classpath:/templates/

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