第一个SpringBoot程序
环境:
- Maven 3.6.0
- jdk1.8
- springboot
- IDEA
官方:提供了一个快速生成springboot的网站!IDEA集成了这个网站
-
SpringBoot中可以快速生成,之后下载一个压缩包,之后IDEA导入就可以了,一个SpringBoot程序生成了
-
IDEA创建SpringBoot程序==(推荐)==
- New Project -> springboot initializr (不是Maven项目) -> 之后next -> dependencies(这个选择Web->spring web) 配置完成
- 分析初始项目的结构
创建的包的规则
在controller下面建立一个类,这个直接返回值就行,不需要创建了一个对应的页面
package com.example.springboot01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//本身就是SpringBoot的一个组件
@Controller
@RestController
public class HelloController {
//接口:http://localhost:8080/hello
@RequestMapping("/hello")
public String hello(){
//调用业务,接受前端的参数
return "hello";
}
}
pom.xml中依赖的作用
<!-- 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- springboot核心依赖,包括tomcat,xml,dispatcherServlet。。。。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot自带测试类,当然用junit也可以 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如何打jar包:
若是遇见错误可以使用以下的代码
<!--
在工作中,很多情况下我们打包是不想执行测试用例的
可能是测试用例不完事,或是测试用例会影响数据库数据
跳过测试用例执
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--跳过项目运行测试用例-->
<skipTests>true</skipTests>
</configuration>
</plugin>
如果打包成功,则会在target目录下生成一个 jar 包
在application.properties中更改服务器的端口号:
server.port:8081