原创

Spring Boot (一) 基础搭建、兼容jar启动及war tomcat启动、集成Swagger2 API。


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


当前里程碑标记地址:

https://github.com/FrameReserve/TrainingBoot/releases



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.training</groupId>
	<artifactId>TrainingBoot</artifactId>
	<version>1.0.0</version>
	<packaging>war</packaging>

	<name>TrainingBoot</name>
	<description>The first Spring Boot project</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

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

		<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-web</artifactId>
		</dependency>
		
		<dependency>  
	        <groupId>org.springframework.boot</groupId>  
	        <artifactId>spring-boot-starter-tomcat</artifactId>  
	        <scope>provided</scope>  
	    </dependency>
		
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.2.2</version>
		</dependency>
		
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.2.2</version>
		</dependency>
		
	</dependencies>

</project>

构建启动类:com.training.SpringBootServlet.java

package com.training;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootServlet extends SpringBootServletInitializer {

	// jar启动
	public static void main(String[] args) {
        SpringApplication.run(SpringBootServlet.class, args);
    }

	// tomcat war启动
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootServlet.class);
    }

}

Swagger REST API生成器,com.training.core.api.Swagger2

package com.training.core.api;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.training"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多Spring Boot相关文章请关注:http://blog.csdn.net/a286352250/")
                .termsOfServiceUrl("http://blog.csdn.net/a286352250/")
                .contact("LuWenhui")
                .version("1.0")
                .build();
    }

}

application.properties

file_path=/home/files/

random_number=${random.int(10)}

spring.profiles.active=dev



Spring boot properties 注解方式,读取application.properties参数,com.training.core.properties.TrainingProperties

package com.training.core.properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TrainingProperties {

	@Value("${file_path}")
    private String filePath;
	
	@Value("${random_number}")
	private Integer number;
	
	@Value("${server.port}")
	private String serverPort;

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public String getServerPort() {
		return serverPort;
	}

	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}
	
}


jar打包,启动方式:

pom.xml

<packaging>jar</packaging>

bat启动脚本:

cd d:
cd D:/projects/TrainingBoot/target
java -jar TrainingBoot-1.0.0.jar --server.port=8080


Tomcat war 发布方式:

pom.xml

<packaging>war</packaging>


启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html

Spring Boot (一) 基础搭建、兼容jar启动及war tomcat启动、集成Swagger2 API。







正文到此结束
Loading...