Spring Boot + Spring Cloud 實現權限管理系統 后端篇(三):搭建開發環境


生成項目模板

登錄Spring Initializr生成Spring Boot項目模板,保存到本地。

地址:https://start.spring.io/

導入Maven項目

使用IDE導入生成的Maven項目,我這里用的是Eclipse。

項目結構很簡單,把不需要的文件清理之后,項目就只有三個文件:

1.pom.xml, Maven的配置文件。

<?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>com.louis</groupId>
    <artifactId>kitty-admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>kitty-admin</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>
        <!-- spring boot -->
        <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>
            </plugin>
        </plugins>
    </build>

</project>

2.application.yml,空的項目配置文件。

3.KittyAdminApplication.java,應用啟動類。

package com.louis.kitty.admin;

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

@SpringBootApplication
public class KittyAdminApplication {

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

僅此而已。如下圖所示。

打包運行

編譯打包

選擇右鍵 pom.xml, run as --> maven install。

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-admin 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ kitty-admin ---
[INFO] ...
[INFO] ...
[INFO] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.697 s
[INFO] Finished at: 2018-08-14T18:08:29+08:00
[INFO] Final Memory: 21M/122M
[INFO] ------------------------------------------------------------------------

啟動應用

選擇右鍵 KittyAdminApplication.java, run as --> Java Application。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2018-08-14 18:01:15.544  INFO 25024 --- [           main] c.l.kitty.admin.KittyAdminApplication    : Starting KittyAdminApplication on GG20J1G2E with PID 25024 (C:\dev\git\kitty\kitty\kitty-admin\target\classes started by 503018338 in C:\dev\git\kitty\kitty\kitty-admin)
... ... ...
2018-08-14 18:01:53.761 INFO 25024 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-08-14 18:01:53.762 INFO 25024 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-08-14 18:01:53.921 INFO 25024 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 159 ms

改變默認端口

Tomcat默認啟動端口是8080,如果需要更換啟動端口可以修改默認配置。

application.yml , port:服務器啟動端口,context-path:指定根路徑對應的目錄。

# Tomcat
server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 1000
        min-spare-threads: 30
    port: 8088
    context-path: /kitty-admin

 如下圖所示,修改之后,啟動端口變成了8088。

自定義Banner

Spring Boot應用啟動后會在控制台輸出Banner信息,默認是顯示 Spring 字樣的Banner,如下圖所示:

 

如果要定制自己的Banner, 只需要在 resources 下放置一個 baner.txt 文件,輸入自己的banner字符即可。

 

Banner字符可以通過類似以下網站生成:

http://patorjk.com/software/taag
http://www.network-science.de/ascii/

重新啟動,可以看到效果:

測試訪問

瀏覽器訪問: localhost:8088, 因為我們還沒提供可訪問內容,所以顯示沒有可映射訪問的內容。

新建一個類來個小小測試,如下圖所示:

 

HelloController.java:

package com.louis.kitty.admin.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping(value="/hello")
    public Object hello() {
        return "Hello Kitty!";
    }
    
}

 

啟動運行,瀏覽器訪問 : localhost:8088/hello , 可以看到服務已經調用成功了。

源碼下載

后端:https://gitee.com/liuge1988/kitty

前端:https://gitee.com/liuge1988/kitty-ui.git


作者:朝雨憶輕塵
出處:https://www.cnblogs.com/xifengxiaoma/ 
版權所有,歡迎轉載,轉載請注明原文作者及出處。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM