简单搭建SpringBoot项目

162 阅读2分钟

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项目了,下面需要创建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文件,启动成功后。

更多spring boot配置项目请查看下载:developer.aliyun.com/article/741…