SpringBoot-hello world
SpringBoot2使用流程:
1.maven设置
<?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.hrf</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
2.引入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <!--导入依赖--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3.创建主程序
/** * 主程序类 * SpringBootApplication:告诉SpringBoot这是一个SpringBoot应用 */ @SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class,args); } }
4.编写业务
/*
*@RestContorller是@Conretller和@ResponseBody两个注释相加的效果一致
*/
@RestController public class HelloController { @RequestMapping("/hello") public String handle01(){ return "hello,SpringBoot2"; } }
5.测试方法
直接运行主程序类
6.SpringBoot配置文件
(application.properties文件名和后缀都不能改变)
配置文件可以修改整个SpringBoot应用
server.port=8888
7.简化部署
(在maven文件中) 报红的话maven清理一下重新打包即可
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
把项目打成jar包,在目标服务器运行即可