重點提要 :
1)@SpringBootApplication--標識是一個springboot項目
SpringApplication.run(啓動類);進行根目錄下啓動
2)@Controller標識是一個controller;
@RequestMapping--url映射;
@ResponseBody--將String字符串返回到頁面上。
(其他:repsonse.getWriter().write();modelAndView;)
1.新建maven工程
<groupId>com.suguo.springboot</groupId>
<artifactId>springboot-assemble</artifactId>
<version>1.0-SNAPSHOT</version>
2.添加依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.編寫啓動類
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
@Controller
public class DemoController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "Hello world";
}
}
注意:這裏一定要新建包,放在自定義的包下,否則會啓動失敗。
錯誤代碼:
demoController也要放在自定義包下,否則會報錯如下:
改正後: