spring-boot从创建到部署(内含swagger配置)

今天介绍一下spring-boot这个开箱即用的框架,从创建到部署

创建spring-boot项目

  • 首先点击creat new project
  • 然后点击spring initializr
  • 输入组名和项目名
  • 选择web
  • finsh完成创建
  • pom.xml配置文件

<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.0modelVersion>

    <groupId>com.stalarygroupId>
    <artifactId>createdemoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>createdemoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        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.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.7.0version>
        dependency>
                <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.7.0version>
        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();
    }
}

这时访问http://120.24.5.178:8100/swagger-ui.html#/即可展示出页面

  • 最后提供一个博主spring-boot的demo
  • https://github.com/stalary/SpringBootDemo

你可能感兴趣的:(技术文章)