# 实验1-springboot
**Repository Path**: chenbairui/experiment1
## Basic Information
- **Project Name**: 实验1-springboot
- **Description**: springboot init
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-03-27
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
##
实验一 使用Spring Boot构建应用程序
课程名称:企业级开发框架专题 学期:2020春季
|
实验名称 | 使用Spring Boot构建应用程序 | 实验序号 | 一 |
|
姓 名 | 陈伯瑞 | 学 号 | 201741412102 | 班 级 | 17软卓1班 |
|
实验地点 | 在家 | 实验日期 | 2020/3/24 | 指导老师 | 黎志雄 |
|
教师评语 | | 实验成绩 | 评阅教师 |
| 百分制 | |
|
同组同学 | 无 |
## 实验目的:
- 掌握使用IntelliJ IDEA创建Spring Boot应用程序的方法;
- 了解spring-boot-starter-parent的配置内容;
- 掌握如何利用Starter扩展Spring Boot应用程序的功能;
- 掌握如何配置Starter;
- 掌握如何通过属性文件定制Spring Boot应用程序的初始化参数;
- 掌握使用Spring Boot编写简单的单元测试;
- 了解Spring Boot应用程序的Fat Jar文件;
- 掌握Markdown轻量级标记语言编写README.md文件。
## 实验环境:
- jdk13
- maven3.6.1
- IntelliJ IDEA
- springboot 2.2.5
## 实验过程
- 在IntelliJ IDEA创建Spring Boot项目,使用maven管理项目,并继承spring-boot-starter-parent 成为springboot项目并在pom文件导 入mvc以及lombok;


- 修改maven的setting.xml配置文件,添加阿里云镜像仓库作为项目maven仓库(也可在pom文件自定义)

### pom.xml文件中主要标签的意义
- 父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括group ID,artifact ID和 version
>` `
- 声明项目描述符遵循哪一个POM模型版本。模型本身的版本很少改变,虽然如此,但它仍然是必不可少的,这是为了当Maven引入了新的特性或者其他模型变更的时候,确保稳定性
>
>
>
>
>
>
>
>
>
>
- 项目所有者自定义的一些属性
> jar
>
> 1.0-SNAPSHOT
>
> banseon-maven
>
> http://www.baidu.com/banseon
>
> A maven project to study maven.
>
>
>
>
>
- 项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。
>
......
- 构建项目需要的信息
>
- 配置jetty作为Spring Boot应用程序的默认Servlet容器,并在配置文件设置jetty端口等信息,并配置编码解决项目中的中文乱码问题


- 在入口Experiment1Application的同级目录的子包下添加一个简单的Spring Mvc控制器组件,用于测试;定义一个CommandLineRunner的Bean,用于检查Spring Boot应用程序启动完成后在Spring IoC容器中注册的所有Bean

package com.springboot.experiment1.controller;
import org.eclipse.jetty.client.HttpResponse;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
/**
* author 陈伯瑞
* date 2020/3/24 0024 10:22
* version 1.0
**/
@RestController
public class helloController {
@RequestMapping(value = "/")
public String index(){
return "你好,Spring Boot!";
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("由springboot注册生成的所有bean:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
- 创建一个单元测试

package com.springboot.experiment1.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.hamcrest.Matchers.equalTo;
@SpringBootTest
@AutoConfigureMockMvc
//@WebMvcTest
class helloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("你好,Spring Boot!")));
}
}
- 运行测试单元,显示测试通过并打印出在Spring IoC容器中注册的所有Bean:

- 也可以通过idea的HTTP Client工具测试控制器端口,创建http脚本进行测试

- 命令行编译打包springboot项目


- 在命令行中使用java命令运行Spring Boot应用程序的Jar文件,带参数改变嵌入式Servlet容器的默认端口8080改为9090(命令行优先级最高)
`java -jar experiment1-0.0.1-SNAPSHOT.jar --debug --server.port=9090`

## 实验总结
1. 单元测试的时候因为导错包(使用idea的快速导包)导致运行结果错误,可见包管理是很重要的
2. 完整运行完springboot项目后使用maven打包也成功之后,无法使用本地的jdk运行jar包,原因在于期间更换了jdk环境而没有配置好(应使用jdk13)