1.啓動intellij,選擇【Create New Project】
2.選擇【Spring Initializr】
3.填寫相關的項目信息
4.選擇需要依賴的模塊,此處入門示例,只需要選擇Web模塊
5.填寫項目名稱相關信息
6.點擊【Finish】後生成的項目架構如下圖所示
7.在默認提供的入口類中,修改相關代碼,加入請求映射註解
- package com.flagship.ch1_1;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class Ch11Application {
- @RequestMapping("/")
- String index(){
- return "hello world,spring boot";
- }
- public static void main(String[] args) {
- SpringApplication.run(Ch11Application.class, args);
- }
- }
8.點擊【Debug Ch11Application】
9.啓動成功後,瀏覽器訪問:http://localhost:8080/,出現如下信息,表示啓動成功
10.代碼解釋:
上述Ch11Application類的示例代碼中,用到的註解,解釋如下
@RestController:可以看作@ResponseBody和@Controller的組合註解
@RequestMapping:用來響應url中"/"的請求映射
@SpringBootApplication:@Configuration,@EnableAutoConfiguration,@ComponentScan的組合註解,其中,@Configuration標識這個類可以使用Spring IoC容器作爲bean定義的來源,@EnableAutoConfiguration會使Spring Boot根據jar包依賴對當前項目進行自動配置,@ComponentScan自動掃描指定包下的全部標有@Component的類(包括其子類),註冊成bean