1. 描述
一个spring boot 的简单demo示例。
环境:
IDE(idea):2021.3
JDK:1.8
maven:3.8.4
spring boot:2.5.6
2. 结果
- 接口返回 Hello {name}!,eg: Hello World!
3. demo
3.0 项目结构
3.1 pom.xml
- 引入 web jar包
<?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>
<parent>
<groupId>com.byrc</groupId>
<artifactId>byrc-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-web</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--提供全栈的 web 开发特性,包括 Spring MVC 依赖和 Tomcat 容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 提供通用单元测试依赖,包括 JUnit, Hamcrest , Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 SpringBootApplication 注解实现启动类
//@ComponentScan(value = "com.demo.web.*")
@SpringBootApplication
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
System.out.println("----- http://localhost:8880/demo/ -----");
}
}
3.3 application.yml配置
- 端口和应用路径
server:
port: 8880
servlet:
context-path: /demo
3.4 其他代码
- TestController.java
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping(value = "/hello", name = "返回Hello World")
public String hello(@RequestParam(value = "name", required = false) String name) {
return String.format("Hello %s! ", name == null ? "World" : name);
}
}
4. 资料
官网:https://spring.io/projects/spring-boot/
官网demo文档:https://spring.io/quickstart
5. 注
- spirng boot 已在父项目引入(父项目pom.xml配置);
- 部分jar包版本已在父项目管理(如果对应不上,一定、肯定、决定是某些修改,没同步更新文档)。
相关文章
暂无评论...