目录
- 1. 在pom.xml中添加依赖
- 2. 编辑resources/application.properties
- 3. 编写springboot的主程序
- 4. 编写Controller程序
- 5. IDEA本地测试
- 6. 打包上传到服务器运行
1. 在pom.xml中添加依赖
步骤如下:
- 添加springboot的parent依赖
- 添加springboot的starter-web依赖。还有很多针对其它场景的starter启动器
- 修改mysql的版本号
- 添加springboot的打包插件
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hh</groupId>
<artifactId>springbootTest</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
</parent>
<properties>
<!-- 修改mysql的版本号 -->
<mysql.version>8.0.25</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${shade.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude></exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/*.DSA</exclude>
<!-- 解决java -classpath ... class运行错误:ERROR StatusLogger Unrecognized format specifier -->
<exclude>**/Log4j2Plugins.dat</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
<resource>reference.conf</resource>
</transformer>
<transformer
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass></mainClass>
<!-- 解决: WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance -->
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. 编辑resources/application.properties
该配置文件的名称是约定好的。修改tomcat的端口号,如下所示:
# 修改tomcat的端口号
server.port = 8088
配置文件的值会绑定对应的类上,类会在IOC容器中创建对象
也可以定义配置文件application.yaml。会和application.properties进行配置合并,但application.properties的优先级更高
3. 编写springboot的主程序
注解说明:
- @SpringBootApplication:表明这是一个springboot应用。会自动扫描同一目录下的其它所有文件夹和文件
如下所示:
package com.hh.springbootTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// springboot的程序入口,表明这是一个springboot应用
// 默认会自动扫描同一目录下的其它所有文件夹和文件
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
4. 编写Controller程序
注解说明:
- @Controller:表明这是一个控制器
- @ResponseBody:表示请求该地址会有数据返回
- @RequestMapping:表示请求的具体地址
- @RestController:等同于@ResponseBody + @Controller
如下所示:
package com.hh.springbootTest.myController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
// @ResponseBody // 也可以放在函数上面,单独修饰函数
// @Controller
@RestController // 等同于:@ResponseBody + @Controller
public class HelloController {
@RequestMapping("/hello")
public String helloHandle() {
return "hello springboot";
}
}
5. IDEA本地测试
直接运行MyApplication.java程序,然后用浏览器访问http://localhost:8088/hello。显示的效果如下:
6. 打包上传到服务器运行
用mvn clean package
命令进行打包,然后将jar包上传到服务器,用java命令java -jar springbootTest-1.0-SNAPSHOT.jar
以前台运行的方式启动应用