diff --git a/.gitignore b/.gitignore index a1c2a23..cce081f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,8 @@ *.tar.gz *.rar +.gradle/ +build/ + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..7d7ed00 --- /dev/null +++ b/build.gradle @@ -0,0 +1,79 @@ +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'idea' + + +def buildJSON = new groovy.json.JsonSlurper().parseText(new File("build.json").text) + +def baseProjectName = buildJSON?.project?.name ?: '__project_name__'; +def shellCommandName = baseProjectName +def eclipseProjectName = baseProjectName +def eclipseProjectComment = buildJSON?.project?.comment ?: '__project_name_comment__' +def jarManifestMainClass = buildJSON?.project?.main ?: 'me.hatter.sample.SampleMain' + +archivesBaseName = buildJSON?.project?.archiveName ?: baseProjectName +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + // mavenCentral() + maven() { url 'https://maven.aliyun.com/repository/central' } +} + +tasks.withType(JavaCompile) { + options.encoding = "UTF-8" +} + +// '-x test' skip unit test +defaultTasks 'build' + +buildscript { + repositories { + mavenLocal() + mavenCentral() + jcenter() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.11.RELEASE") + } +} +apply plugin: 'org.springframework.boot' +springBoot { + mainClass = jarManifestMainClass +} + +dependencies { + compile files(fileTree(dir: 'lib', includes: ['*.jar'], excludes: ['*-sources.jar', '*-javadoc.jar'])) + + if (buildJSON.repo != null && buildJSON.repo.dependencies != null) { + buildJSON.repo.dependencies.each { + compile("${it}") + } + } +} + +eclipse { + project { + name = eclipseProjectName + comment = eclipseProjectComment + } + classpath { + defaultOutputDir = file('classes') + downloadSources = true + file { + whenMerged { classpath -> + classpath.entries.findAll { it.kind=='lib' }.each { + if ((it.path != null) && (it.sourcePath == null) && file(it.path.replace(".jar", "-sources.jar")).exists()) { + it.sourcePath = getFileReferenceFactory().fromPath(it.path.replace(".jar", "-sources.jar")) + } + } + } + } + } +} + +eclipseJdt << { + File f = file('.settings/org.eclipse.core.resources.prefs') + f.write('eclipse.preferences.version=1\n') + f.append('encoding/=utf-8') +} diff --git a/build.json b/build.json new file mode 100644 index 0000000..435b683 --- /dev/null +++ b/build.json @@ -0,0 +1,20 @@ +{ + "project": { + "name": "springbootswaggersample", + "main": "me.hatter.sample.springbootswagger.Application", + "archiveName": "sprintbootswaggersample" + }, + "java": "1.8", + "builder": { + "name": "gradle", + "version": "3.1" + }, + "repo": { + "dependencies": [ + "org.springframework.boot:spring-boot-starter:1.3.2.RELEASE", + "org.springframework.boot:spring-boot-starter-web:1.3.2.RELEASE", + "io.springfox:springfox-swagger2:2.2.2", + "io.springfox:springfox-swagger-ui:2.2.2" + ] + } +} diff --git a/src/main/java/me/hatter/sample/springbootswagger/Application.java b/src/main/java/me/hatter/sample/springbootswagger/Application.java new file mode 100644 index 0000000..916f3d5 --- /dev/null +++ b/src/main/java/me/hatter/sample/springbootswagger/Application.java @@ -0,0 +1,12 @@ +package me.hatter.sample.springbootswagger; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/me/hatter/sample/springbootswagger/Swagger2.java b/src/main/java/me/hatter/sample/springbootswagger/Swagger2.java new file mode 100644 index 0000000..fc6f489 --- /dev/null +++ b/src/main/java/me/hatter/sample/springbootswagger/Swagger2.java @@ -0,0 +1,37 @@ +package me.hatter.sample.springbootswagger; + +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("me.hatter.sample.springbootswagger.web")) + .paths(PathSelectors.any()) + .build(); + } + + + ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("Springboot swagger sample") + .description("Tthe Springboot swagger sample") + .termsOfServiceUrl("https://hatter.ink/") + .contact("Hatter") + .version("1.0.0") + .build(); + } +} diff --git a/src/main/java/me/hatter/sample/springbootswagger/web/SampleController.java b/src/main/java/me/hatter/sample/springbootswagger/web/SampleController.java new file mode 100644 index 0000000..a87bf62 --- /dev/null +++ b/src/main/java/me/hatter/sample/springbootswagger/web/SampleController.java @@ -0,0 +1,17 @@ +package me.hatter.sample.springbootswagger.web; + +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/sample") +public class SampleController { + + @ApiOperation(value = "Hello", notes = "Say Hello World!") + @RequestMapping(value = "", method = RequestMethod.GET) + public String hello() { + return "Hello world!"; + } +}