SpringBoot(一)入門

說明

本篇針對小白,從0到1搭建一個boot運行環(huán)境

前置條件

本地已經(jīng)安裝好jdk8、maven3.4及以上的版本、IDEA開發(fā)工具

步驟

新建工程

打開IDEA,新建bootStart工程,菜單:File->New->Project


新建boot工程導(dǎo)航截圖.png

其余步驟按照提示操作即可!

添加pom依賴

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>bootStart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>bootStart</name>
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

添加啟動類

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

添加controller

package org.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StartController {

    @RequestMapping("/hello")
    public String hello() {
        return "{\"key\":\"tom\",\"value\":\"tom\"}";
    }
}

運行項目

執(zhí)行StartApplication的main方法,觀察控制臺有啟動信息,待出現(xiàn)如下信息時,標明啟動成功

......
2021-11-03 21:24:36.497  INFO 66169 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 786 ms
2021-11-03 21:24:36.864  INFO 66169 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-03 21:24:36.879  INFO 66169 --- [           main] org.example.StartApplication             : Started StartApplication in 1.643 seconds (JVM running for 2.172)

測試

在瀏覽器中輸入: http://localhost:8080/hello 回車出現(xiàn)

{"key":"tom","value":"tom"}

標明服務(wù)可用

至此一個簡單的服務(wù)啟動成功。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容