簡單搭建SpringBoot項目

Spring Boot 簡單介紹

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

構建SpringBoot項目

  • 構建以前先介紹下個人環境:

編譯器 :idea2017.1.2 JDK:1.8.0_77html

建立Maven工程

寫入ArtifactId和GroupId

寫入ProjectName

Maven項目建立完成

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

pom.xml文件:git

<?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:github

//端口
server.port=8080

html、css 等文件格式:web

文件結構

ExampleApplication.java:spring

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:apache

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:api

<!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之旅

相關文章
相關標籤/搜索