spring boot(学习笔记第一课)

spring boot(学习笔记第一课)


学习内容:

  1. IDE (IntelliJ IDEA)
  2. spring boot起步 https://start.spring.io/
  3. git代码管理
  4. hello,world
  5. 增加hello controller

1. IDE(IntellJ IDEA)

IntellJ IDEA是目前最流行的Java开发IDE,在开发中较为常用。我们采用免费的Community(社区版)
IDEA Community版

2. spring boot起步

spring boot starter
我们指定:Project=Maven,Language=Java.基本上保持默认。
在这里插入图片描述

3. git代码管理

代码管理我们采用gitee.com来进行。
GIT代码托管库

  1. 注册账号。
  2. 生成公钥密钥对。
    ssh-keygen -t rsa
    id_rsa 私钥文件(不能给其他人,只是本地的使用)
    id-rsa.pub 登录给gitee.com
    在这里插入图片描述
  3. 之后将公钥文件登录到gitee.com自己的账号中。
    右上角->下拉菜单->账号设定->安全设定->SSH公钥
    在这里插入图片描述
  4. 创建一个spring boot代码库进行代码管理。
    在这里插入图片描述
  5. 利用git bash进行代码的下载。
    git clone git@gitee.com:finlay-xiao/spring-boot-hello.git
    这里的用户名改成自己的

4. hello,world

  1. 将步骤2下载的解压缩,之后放到上面的git工程里面。
    在这里插入图片描述
  2. 将最初版的spring boot hello程序push到git仓库。
  3. 使用IntelliJ IDEA将spring-boot-hello文件夹打开。
    在这里插入图片描述
  4. 增加我们的第一个hello,world controller
    注意我们一定要将各种组件component,configuration和controller的类放在DemoApplication的子包下面。
    例如,我们的例子中:
    主类所在包是: com.example.demo.DemoApplication;
    Controller类的所在包是: com.example.demo.Controller;
    如果不是子包结构的话,DemoApplication类的@SpringBootApplication注解不会就行扫描,也就是不会将controller纳入到我们的SpringBoot工程中。

如果我们是在不希望用这种默认的子包的方式,那么可以指定scan报名的方式。这样的话就没有任何限制了。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication(scanBasePackages = {"com.example.package1", "com.example.package2"})
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

之后我们开始Controller的编码。

  1. 增加hello controller
  2. 对于pom.xml文件增加依赖
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
</dependency>

增加完了类的依赖之后,要对IntelliJ IDEA进行maven的刷新。
在这里插入图片描述
2. 编写我们的controller类
这里必须用RestController!

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "hello,world";
    }
}

如果在有的类IntelliJ IDEA找不到,那么可以用快捷键Alt-Enter进行快速的import。
3. 找到main class,执行启动。
在这里插入图片描述
4.这样就出现这样的log,正常启动了。

 Tomcat initialized with port 8080 (http)
 Tomcat started on port 8080 (http) with context path '/'

这里默认是http协议,并且是8080端口。
5.打开浏览器,进行访问。
这里必须用http访问,目前的浏览器默认都是https,所以注意输入
在这里插入图片描述
浏览器中的第一个hello,world就出来了。
在这里插入图片描述

学习视频

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值