新しいプロジェクトを作成します。1.プロジェクトノートは戦闘の役割を説明します
コンフィギュレーションに依存2.pom.xml
< プロパティ> < project.build.sourceEncoding > UTF-8 </ project.build.sourceEncoding > </ プロパティ> < 親> < のgroupId > org.springframework.boot </ のgroupId > < たartifactId >ばねブートスタータ親</ たartifactId > < バージョン> 2.0.1.RELEASE </ バージョン> </ 親> < 依存性> <依存関係> < groupIdを> org.springframework.boot </ groupIdを> < たartifactId >春・ブート・スターター・ウェブ</ たartifactId > </ 依存関係> </ 依存関係>
コンフィギュレーション・プログラムエントリ3.Application.java
パッケージxiaobing.demo。 輸入org.springframework.boot.SpringApplication。 輸入org.springframework.boot.autoconfigure.SpringBootApplication。 @SpringBootApplication パブリック クラスアプリケーション{ 公共 静的 ボイドメイン(文字列[]引数){ SpringApplication.run(応用クラス、引数)。 } }
4.demoマップ
パッケージxiaobing.demo.controller。 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController; @RestController パブリック クラスの例{ @RequestMapping( "/" ) 文字列の自宅(){ リターンの"Hello World!" ; } }
5.詳細なメモ
5.1 @SpringBootApplication = @コンフィギュ+ @ EnableAutoConfiguration + @ ComponentScan
@SpringBootApplication パブリック クラスアプリケーション{ 公共 静的 ボイドメイン(文字列[]引数){ SpringApplication.run(応用クラス、引数)。 } } // 等同于: @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan パブリック クラスアプリケーション{ 公共 静的 ボイドメイン(文字列[]引数){ SpringApplication.run(アプリケーションクラス、引数)。 } }
5.2 @RestControllerと@RequestMappingは、springMVCノートでコメントspringbootに固有のものではありません
即@RestController = @コントローラー+ @ ResponseBody
@RestController public class Example { @RequestMapping("/") String home() { return "Hello World!"; } @RequestMapping("/test") public Map<String, String> map(){ Map<String, String> map = new HashMap<String, String>(); map.put("name","张三"); map.put("age","25"); return map; } } //等同于: @Controller public class Example { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } @RequestMapping("/test") @ResponseBody public Map<String, String> map(){ Map<String, String> map = new HashMap<String, String>(); map.put("name","张三"); map.put("age","25"); return map; } }
6.启动项目
7.后续补充