SpringBoot HelloWorld

項(xiàng)目代碼:https://github.com/daydreamer1988/springboot-helloworld

方法一:使用Spring Initializer

New Project
選擇構(gòu)建類型
選擇需要的依賴
解析下載依賴
初始目錄結(jié)構(gòu)

pom.xml

<?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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.minicup</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
           
                // 支持Maven打包
            </plugin>
        </plugins>
    </build>

</project>

Application


@SpringBootApplication
public class SpringbootHelloworldApplication {

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

以上就是所有的配置,運(yùn)行main函數(shù)

image.png

打開瀏覽器http://localhost:8080

image.png

服務(wù)是啟動(dòng)了, 但是還沒有接口,接下來創(chuàng)建HelloController.java

@RestController
public class HelloController {

    @GetMapping("/hello/{name}")
    public String hollo(@PathVariable String name){
        return "Hello "+ name;
    }
}

重新運(yùn)行

image.png

除了運(yùn)行main函數(shù)之外, 還可以通過命令行執(zhí)行mvn spring-boot:run來啟動(dòng)

image.png

項(xiàng)目打包部署

image.png
image.png

在命令行中輸入java -jar target/springboot-helloworld-0.0.1-SNAPSHOT.jar

image.png

注意, pom文件中的依賴沒有版本號(hào), 這是因?yàn)閜arent的spring-boot-starter-parent中已經(jīng)定義了

方法二:使用普通Maven創(chuàng)建SpringBoot

image.png
image.png
目錄結(jié)構(gòu)

pom.xml

image.png

添加以下依賴(https://spring.io/guides/gs/rest-service/)

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

添加Application類

@SpringBootApplication
public class Application {

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

添加TestController類

@RestController
public class TestController {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "Hello " + name;
    }
}

運(yùn)行main函數(shù),訪問8080,完成

方法三:官網(wǎng)下載類似腳手架項(xiàng)目,IDEA直接打開即可

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

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

  • 1.安裝maven 2.eclipse安裝springboot插件 3.修改eclipse中maven路徑 以上三...
    W_c445閱讀 605評論 0 0
  • 使用IntelliJ IDEA 新建一個(gè)演示項(xiàng)目1、打開IDEA,選擇新建項(xiàng)目,然后在左側(cè)選擇“Spring In...
    小昭文閱讀 730評論 1 1
  • 1、Spring Boot 簡介 簡化Spring應(yīng)用開發(fā)的一個(gè)框架; 整個(gè)Spring技術(shù)棧的一個(gè)大整合; J2...
    瑾蘭閱讀 415評論 0 3
  • jHipster - 微服務(wù)搭建 CC_簡書[http://m.itdecent.cn/u/be0d56c4...
    quanjj閱讀 936評論 0 2
  • “隨風(fēng)潛入夜,潤物細(xì)無聲”,春雨不聲不響,沒有雪花的圣潔美麗,沒有暴雨的威猛,她的美卻是獨(dú)特的,在不聲不響中滋...
    江左劉樓小學(xué)王閱讀 1,228評論 0 0

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