# SpringBootClass1 **Repository Path**: li_tiger_admin/SpringBootClass1 ## Basic Information - **Project Name**: SpringBootClass1 - **Description**: springboot第一次作业 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-03-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 一、通过IntelliJ IDEA的Spring Initializr向导创建Spring Boot项目。 ##### 1.选择Spring Initializr,并制定所使用的jdk版本。 ![](https://images.gitee.com/uploads/images/2020/0323/222528_0fd6f779_4853686.png "image-20200323145737044.png") ##### 2.添加springweb ![](https://images.gitee.com/uploads/images/2020/0323/222809_178e2ca9_4853686.png "image-20200323151026786.png") ##### 3.在setting中修改一下maven,改用本地maven,指定本地仓库地址。 ![](https://images.gitee.com/uploads/images/2020/0323/223058_f5abe8e6_4853686.png "image-20200323154327303.png") ##### 4.查看pom.xml文件 ![](https://images.gitee.com/uploads/images/2020/0323/223146_a129ef44_4853686.png "image-20200323154129816.png") ![](https://images.gitee.com/uploads/images/2020/0323/223246_c2ec5e12_4853686.png "image-20200323154619313.png") ### 二、添加和修改响应依赖和配置。 ##### 1.添加lombok依赖。 ​ 在pojo类上就可以@Getter @Setter 和@Data注解,直接在中添加如下代码即可。 ```xml org.projectlombok lombok ``` ##### 2.添加阿里云镜像仓库作为项目maven仓库。 ​ 为了让maven提升自动下载依赖时的速度,可以在标签里加入下面的代码,这样我们就不需要在settings.xml里设置mirrors,这样做还有一个好处,就是让我们的配置不依赖于个人的配置: ```xml AliYun-public public https://maven.aliyun.com/repository/public ``` ##### 3.配置jetty作为Spring Boot应用程序的默认Servlet容器。 ​ 默认的嵌入式 Web 服务器容器是Tomcat。只需要在中添加下面的依赖即可 ```xml org.springframework.boot spring-boot-starter-jetty ``` ### 三、添加一个简单的Spring Mvc控制器组件,用于测试。 ##### 1.创建控制类controller。 ​ 在test1创建Controller包,在里面创建HelloController.java。代码如图所示。在类名上添加@Controller注解,在方法上添加@ResponseBody和RequestMapping("/hello"),也可以用@RestController代替@Controller,就可以省去@ResponseBody ![](https://images.gitee.com/uploads/images/2020/0323/223348_ed3a854a_4853686.png "image-20200323152827372.png") ##### 2.查看所有的bean。 ​ 在test1下创建一个Config包,在里面创建MyConfig.java。定义一个CommandLineRunner的Bean,用于检查Spring Boot应用程序启动完成后在Spring IoC容器中注册的所有Bean。如下图: ![](https://images.gitee.com/uploads/images/2020/0323/223424_1cf179ed_4853686.png "image-20200323175158932.png") ##### 3.在测试类中编写如下代码进行测试; ![](https://images.gitee.com/uploads/images/2020/0323/223510_e8563d3f_4853686.png "image-20200323183200261.png") ### 四、采用多种形式进行测试。 ##### 1.修改默认的端口号。 ​ 服务器默认的端口号为8080,修改为8888;只需在application.properties中添加如下代码: ```html server.port=8888 ``` ##### 2.在浏览器访问。 ​ 直接在浏览器输入localhost:8888/hello ![](https://images.gitee.com/uploads/images/2020/0323/223544_f6b30806_4853686.png "image-20200323200251340.png") ##### 3.在RestServices中运行。 ​ 需要先安装插件RestfulToolkit ![](https://images.gitee.com/uploads/images/2020/0323/223626_9f0c4da5_4853686.png "image-20200323200610410.png") 然后在右边打开RestServices,点击相应的页面,点击Send即可。 ![](https://images.gitee.com/uploads/images/2020/0323/223703_7466a2d7_4853686.png "image-20200323200926124.png") ##### 4.在http文件中运行。 首先在项目中创建一个http文件,然后输入如下代码,点击运行即可: ```xml GET http://localhost:8888/hello ``` ![](https://images.gitee.com/uploads/images/2020/0323/223734_d6f7a102_4853686.png "image-20200323201355990.png") ##### 5.运行jar包。 首先点击package生成jar包。 ![](https://images.gitee.com/uploads/images/2020/0323/223806_876a31ee_4853686.png "image-20200323202702828.png") 然后可以打开cmd在相应目录,运行java -jar命令,在浏览器输入网址即可! ![](https://images.gitee.com/uploads/images/2020/0323/223836_7694d828_4853686.png "image-20200323202916628.png") ### 五、上传到码云。 ##### 1.在码云创建一个新的仓库。 ![](https://images.gitee.com/uploads/images/2020/0323/223915_cd029008_4853686.png "image-20200323210220698.png") ##### 2.创建本地仓库并上传。 选择 VCS--->Enable Version Control... ![](https://images.gitee.com/uploads/images/2020/0323/224001_89f458b6_4853686.png "image-20200323212423209.png") 选择git,然后项目会变红,下面会有一个Version Control.里面都是没有保存到缓存区的,一些文件不需要上传。 ![](https://images.gitee.com/uploads/images/2020/0323/224039_e39743f8_4853686.png "image-20200323212625313.png") ![](https://images.gitee.com/uploads/images/2020/0323/224106_73971800_4853686.png "image-20200323212916180.png") 选择项目右键Git ---> Commit Directory...,然后选择上传,点击commit即可。上传本地仓库成功! ![](https://images.gitee.com/uploads/images/2020/0323/224150_e72f2999_4853686.png "image-20200323214904301.png") ![](https://images.gitee.com/uploads/images/2020/0323/224215_c1c53729_4853686.png "image-20200323215309706.png") ##### 3.上传push到远程仓库。 选择 VCS --->Git --->Push ![](https://images.gitee.com/uploads/images/2020/0323/224240_d5ba06d5_4853686.png "image-20200323220107918.png") ![](https://images.gitee.com/uploads/images/2020/0323/224301_5b38b7df_4853686.png "image-20200323220347115.png") ![](https://images.gitee.com/uploads/images/2020/0323/224323_a18edf59_4853686.png "image-20200323220522499.png")