SpringBoot學習_使用嚮導快速建立Spring Boot應用
阿新 • • 發佈:2018-11-06
新建專案
起包名模組名等
選擇模組(其實就是選擇starters)
例如我要匯入校驗功能選勾Validation他就會匯入JSR-303了
選最基本的web
然後下一步再finish就可以了
然後它就會聯網從springboot官網幫我們生成專案
生成的專案中已經有一個主程式了,我們就可以專心寫controller等了
package com.nyh.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework. web.bind.annotation.RestController;
/**
* @author nyh
* @create 2018-10-12 10:44
**/
@RestController
public class HelloWorld {
@RequestMapping("/hello")
public String hello(){
return "hello quick";
}
}
@RestController也是組合註解,這樣我們就不用每次都要寫@Controller和@ResponseBody註解了
@Controller:控制器
@ResponseBody的作用:返回資料直接寫給瀏覽器,如果是物件還能轉為json資料返回
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
然後就可以執行主程式在瀏覽器進行訪問了
預設生成的Spring Boot專案中
主程式已經生成好了,我們只需要寫我們自己的邏輯
- resources資料夾中目錄結構
- static:儲存所有的靜態資源; 放 js css images等;
- templates:儲存所有的模板頁面;(Spring Boot預設jar包使用嵌入式的Tomcat,預設不支援JSP頁面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot應用的配置檔案;可以修改一些預設設定;比如修改server埠號為8081,修改後重新執行