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

生成項目模板

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

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

導入Maven項目

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

項目結構很簡單,把不須要的文件清理以後,項目就只有三個文件:git

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

<?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,空的項目配置文件。spring

3.KittyAdminApplication.java,應用啓動類。apache

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。tomcat

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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/ 版權全部,歡迎轉載,轉載請註明原文做者及出處。

相關文章
相關標籤/搜索