SpringBoot>01-第一个应用--HelloWorld

简介:

此章介绍一个最基本的SpringBoot Web 应用。体验快速开发、无配置,以及基本的注解。

个人学习总结:
链接:【springboot、springcloud、docker 等,学习目录】

环境:

1、JDK 1.8
2、springboot 1.5
3、开发工具 IDEA 或 STS(下载地址:https://spring.io/tools)
4、Maven (3.3.9)注意配置阿里云镜像,具体配置根据自身开发要求查阅资料。

快速构建:

通过官网地址快速构建:https://start.spring.io/ 如下图
SpringBoot>01-第一个应用--HelloWorld_第1张图片
springboot的版本根据开发环境选择,本文选择1.5。解压快速生成的压缩包导入开发工具即可。

1. 主要 pom 依赖如下

根据自己开发需求选择版本、添加依赖:

 
     org.springframework.boot
     spring-boot-starter-parent
     1.5.18.BUILD-SNAPSHOT
      
 
 
 
     UTF-8
    UTF-8
    1.8



    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

2. 程序入口类:

 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 /**
  * @Auther: xf
  * @Date: 2018/10/01 13:48
  * @Description:  入口程序
  */
 @SpringBootApplication
 public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
   }
}

@SpringBootApplication 注解: 声明此类为该springboot application 入口类,一个模块有且仅有一个此注解。

3. 测试controller:

 // @RestController 注解: 相当于 @Controller + @ResponseBody
 @RestController
 public class HelloController {
 
    // @RequestMapping(value = "hello", method = RequestMethod.GET)
    // 等价上面
    @GetMapping(value = "hello")
    public String index() {
        return "Who is your little fox, and who is your rose?";
    }
}

4. 配置文件:

src\main\resources下: application.properties(或者 .yml)

# 项目路径默认/
server.context-path=/demo
# 端口号默认8080
server.port=9000

5. 项目启动、测试:

运行入口类 main 函数即可启动。
Get请求可以使用浏览器输入:http://localhost:9000/demo/hello

至此,一个最简单的 SpringBoot 应用创建完成。

个人微信公众号,谢谢支持!
SpringBoot>01-第一个应用--HelloWorld_第2张图片

你可能感兴趣的:(SpringBoot,系列一,SpringBoot系列一)