当前位置: 移动技术网 > IT编程>开发语言>Java > [学习笔记] SpringBoot 之 Helloworld

[学习笔记] SpringBoot 之 Helloworld

2020年03月21日  | 移动技术网IT编程  | 我要评论

创建项目

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网