本文主要介绍springboot项目搭建的相关demo。
环境依赖
- ide:idea;
- java版本:jdk1.8;
- springboot版本:2.3.9.RELEASE;
- maven版本:3.6.3;
- 操作系统:windows10 64位。
一、创建项目
-
打开idea-file-new-project;
-
选择maven项目,点击next按钮;
-
设置程序名称、目录以及GroupId及ArtifactId等相关配置,点击finish按钮生成project;
pom.xml
引入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>ichpeng.springboot.demo</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-demo</name>
<description>springboot初始化样例</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
创建测试接口
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: ichpeng@qq.com
* @date: 2021/3/16 16:39
* @description:
*/
@RestController
public class TestController {
@RequestMapping("/test")
public Object testApplication(){
return "the application is ok!";
}
}
创建启动程序
创建DemoApplication.java文件,在类加@SpringBootApplication注解,添加main方法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author: ichpeng@qq.com
* @date: 2021/3/16 16:10
* @description:
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("SpringBoot程序启动。。。");
}
}
启动程序
运行main方法即可,默认端口8080。
访问http://127.0.0.1:8000/test
配置文件
springboot支持.properties、.yml等格式配置文件(以application.yml为例)
在resources目录下创建application.yml配置文件
- 修改端口号:
server:
port: 8000
- 定义程序名称:
spring:
application:
name: spring-boot-demo
重启查看效果: