簡單搭建SpringBoot項目

Spring Boot 簡單介紹

Spring Boot 本身並不提供Spring框架的核心特性以及擴展功能,只是用於快速、敏捷地開發新一代基於Spring框架的應用程序。也就是說,它並不是用來替代Spring的解決方案,而是和Spring框架緊密結合用於提升Spring開發者體驗的工具,同時它集成了大量常用的第三方庫配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot應用中這些第三方庫幾乎可以零配置的開箱即用(out-of-the-box),大部分的Spring Boot應用都只需要非常少量的配置代碼,開發者能夠更加專注於業務邏輯

構建SpringBoot項目

  • 構建之前先介紹下我的環境:

編譯器 :idea2017.1.2
JDK:1.8.0_77

創建Maven工程

寫入ArtifactId和GroupId

寫入ProjectName

Maven項目創建完成

完成上面的步驟就已經構建了一個Maven項目了,下面需要創建SpringBoot的配置文件

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>

    <groupId>Example-SpringBoot-Swagger-Group</groupId>
    <artifactId>Example-SpringBoot-Swagger</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

</project>
  • 配置Resource文件夾下面的文件
  • static:文件下面存放的是 css js image 等靜態資源
  • templates:存放的是html文件,這兩個文件都是默認無需配置路徑的就可以直接引用的
  • application.properties:文件配置的是SpringBoot工程的相關信息
  • ExampleApplication.java:SpringBoot的啓動類,需要放在其他類的外面

application.properties:

//端口
server.port=8080

html、css 等文件格式:

文件結構

ExampleApplication.java:

package com.example;

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

/**
 * Created by shuai on 2017/5/21.
 */
@SpringBootApplication
public class ExampleApplication {

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

}

DemoController.java:

package com.example.controller;

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

/**
 * Created by shuai on 2017/5/21.
 */
@Controller
@RequestMapping(value = "/api/demo")
public class DemoController {
    @RequestMapping(value = "/welcome")
    public String demoReturnSuccess(){
        return "welcome";
    }
}

welcome.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>歡迎來到SpringBoot</title>
</head>
<body>
    <h1>歡迎來到SpringBoot</h1>
</body>
</html>

到此簡單的SpringBoot項目就已經創建成功了,運行 ExampleApplication.java文件,啓動成功後。
在瀏覽器中輸入:http://localhost:8080/api/demo/welcome 就可以看見 templates 文件下的welcome.html文件內容

github地址:Spring Boot 教程、技術棧、示例代碼

聯繫我

掃描關注公衆號:java之旅

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章