1. 程式人生 > >spring-boot從建立到部署(內含swagger配置)

spring-boot從建立到部署(內含swagger配置)

今天介紹一下spring-boot這個開箱即用的框架,從建立到部署

建立spring-boot專案

  • 首先點選creat new project
  • 然後點選spring initializr
  • 輸入組名和專案名
  • 選擇web
  • finsh完成建立
  • 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>com.stalary</groupId> <artifactId>createdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging
>
<name>createdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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> <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> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
  • 此時會發現在target目錄下有了loginDemo-0.0.1-SNAPSHOT.jar檔案
  • 將這個檔案上傳到伺服器上,伺服器上要確保已經成功安裝jdk。
  • 使用nohup java -jar loginDemo-0.0.1-SNAPSHOT.jar即可執行專案(加nohup代表一直執行,不會停止,否則關閉就會停止專案)
  • 使用tail -f nohup.out即可檢視動態日誌
  • 或者使用java -jar loginDemo-0.0.1-SNAPSHOT.jar > log.file 2>&1 &(有時候斷開與伺服器連線,專案還是會停止)

如果需要在專案上部署swagger,請看下面的教程

新增swagger2

  • 首先在pom.xml中新增兩個依賴
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
                <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
  • 然後新增一個Sagger2配置類
package com.stalary;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("demo")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.stalary.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("登陸測試模組")
                .description("原始碼請訪問:https://github.com/stalary/SpringBootDemo")
                .termsOfServiceUrl("stalary.com")
                .version("1.0")
                .build();
    }
}