springboot學習第一篇(建立helloworld)

1、目標:瀏覽器發送hello world 請求,服務器響應並向瀏覽器發送「hello world」。web

一、建立maven工程spring

 二、pom.xml 中導入相關依賴瀏覽器

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

三、編寫主程序:啓動springboot應用springboot

(1)涉及的註解:服務器

@springbootapplycation 說明該類爲springboot主程序app

package com.antiguigu;

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

@SpringBootApplication
public class HelloWorldApplycationMain {

    public static void main(String[] args) {
        //啓動spring應用
        SpringApplication.run(HelloWorldApplycationMain.class,args);
    }
}

四、編寫controller、service:業務邏輯代碼。maven

package com.antiguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HellorWorldCon {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }

}

 五、測試應用spring-boot

運行 HelloWorldApplycationMain  中的main 方法。在頁面中輸入地址 http://localhost:8080/hello 訪問服務。測試

 

2、springboot部署ui

在pom.xml 文件中添加

    <!-- springboot 打包插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在IDEA中將項目打包爲jar文件後在cmd窗口使用命令 Java -jar 運行jar文件。

相關文章
相關標籤/搜索