[学习笔记] SpringBoot 之 Helloworld

创建项目

IDEA / File / New / Project / Spring Initalizr / Next

Group: com.wu            公司官网域名反写
Artifact: helloworld     项目的名字,项目根目录的名称
Version: 0.0.1-SNAPSHOT  当前的版本,SNAPSHOT为开发版,Release为稳定版
Name: helloworld         启动类的类名
Package: com.wu.helloworld    包名
Dependencies: Web / Spring Web  引入 tomcat,dispatcherServlet,xml 等依赖
              Developer Tools / Spring Boot DevTools  热部署工具,可选

项目主要文件

程序主入口:

src/main/java/com/wu/helloworld/HelloworldApplication

核心配置文件:

src/main/resources/application.properties

单元测试:

src/test/java/com/wu/helloworld/HelloworldApplicationTests

依赖配置文件:

pom.xml

pom.xml 说明

<!-- 父依赖 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <!-- web场景启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot单元测试 -->
    <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>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <!-- 跳过项目运行测试用例 -->
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

编写代码

创建目录:

src/main/java/com/wu/helloworld/controller

src/main/java/com/wu/helloworld/dao

src/main/java/com/wu/helloworld/pojo

src/main/java/com/wu/helloworld/service

# src/main/java/com/wu/helloworld/controller/HelloController

@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/world")
    public String world() {
        return "Hello, world!";
    }
}

测试运行

执行打包:

Maven Projects / helloworld / Lifecycle / package

打包目录:

target/helloworld-0.0.1-SNAPSHOT.jar

运行程序:

target> java -jar .\helloworld-0.0.1-SNAPSHOT.jar

访问测试:

http://localhost:8080/hello/world

猜你喜欢

转载自www.cnblogs.com/danhuang/p/12532612.html