diff --git a/.gitignore b/.gitignore index d99a1b1..94b7950 100644 --- a/.gitignore +++ b/.gitignore @@ -1,53 +1,10 @@ -# ---> Java -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# ---> macOS -# General +build +classes .DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - +.gradle +.classpath +.project +.settings +*.iml +*.ipr +*.iws \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..abcb4ab --- /dev/null +++ b/build.gradle @@ -0,0 +1,92 @@ +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'idea' + +def JsonSlurper = Class.forName('groovy.json.JsonSlurper'); +def buildJSON = JsonSlurper.newInstance().parseText(new File("build.json").text) + +if (buildJSON.application) { apply plugin: 'application' } + +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 ?: 'SampleMain' + +if (buildJSON.application) { mainClassName = jarManifestMainClass } +archivesBaseName = buildJSON?.project?.archiveName ?: baseProjectName +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +def addRepo = new File(System.getProperty("user.home"), ".build_add.repo") + +repositories { + mavenLocal() + // mavenCentral() + maven() { url 'https://maven.aliyun.com/repository/central' } + if (addRepo.exists()) { maven() { url addRepo.text.trim() } } +} + +tasks.withType(JavaCompile) { + options.encoding = "UTF-8" +} + +// '-x test' skip unit test +defaultTasks 'build' + +buildscript { + repositories { + mavenLocal() + maven() { url 'https://maven.aliyun.com/repository/central' } + 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}") + } + } + if (buildJSON.repo != null && buildJSON.repo.testDependencies != null) { + buildJSON.repo.testDependencies.each { + testCompile("${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..3d34023 --- /dev/null +++ b/build.json @@ -0,0 +1,22 @@ +{ + "project": { + "name": "spring-boot-sample", + "main": "me.hatter.sample.Application", + "archiveName": "spring-boot-sample" + }, + "application": false, + "java": "1.8", + "builder": { + "name": "gradle", + "version": "4.10" + }, + "repo": { + "dependencies": [ + "me.hatter:commons:3.0", + "org.springframework.boot:spring-boot-starter-web:2.3.12.RELEASE" + ], + "testDependencies": [ + "junit:junit:4.12" + ] + } +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..4eba6ad --- /dev/null +++ b/justfile @@ -0,0 +1,5 @@ +_: + @just --list + +run: + buildj bootRun diff --git a/src/main/java/me/hatter/sample/Application.java b/src/main/java/me/hatter/sample/Application.java new file mode 100644 index 0000000..1683587 --- /dev/null +++ b/src/main/java/me/hatter/sample/Application.java @@ -0,0 +1,28 @@ +package me.hatter.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@ComponentScan(basePackages = "me.hatter.sample") +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +// @Bean +// public CommandLineRunner commandLineRunner(ApplicationContext ctx) { +// return args -> { +// +// System.out.println("Let's inspect the beans provided by Spring Boot:"); +// +// String[] beanNames = ctx.getBeanDefinitionNames(); +// Arrays.sort(beanNames); +// for (String beanName : beanNames) { +// System.out.println(beanName); +// } +// +// }; +// } +} diff --git a/src/main/java/me/hatter/sample/common/WebConfig.java b/src/main/java/me/hatter/sample/common/WebConfig.java new file mode 100644 index 0000000..d662b15 --- /dev/null +++ b/src/main/java/me/hatter/sample/common/WebConfig.java @@ -0,0 +1,21 @@ +package me.hatter.sample.common; + +import me.hatter.sample.common.resolver.SystemTimeArgumentResolver; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.*; + +import java.util.List; + +@Configuration +public class WebConfig extends WebMvcConfigurerAdapter { + + @Autowired + SystemTimeArgumentResolver systemTimeArgumentResolver; + + @Override + public void addArgumentResolvers(List argumentResolvers) { + argumentResolvers.add(systemTimeArgumentResolver); + } +} diff --git a/src/main/java/me/hatter/sample/common/annotation/SystemTime.java b/src/main/java/me/hatter/sample/common/annotation/SystemTime.java new file mode 100644 index 0000000..73e7a9a --- /dev/null +++ b/src/main/java/me/hatter/sample/common/annotation/SystemTime.java @@ -0,0 +1,9 @@ +package me.hatter.sample.common.annotation; + +import java.lang.annotation.*; + +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface SystemTime { +} diff --git a/src/main/java/me/hatter/sample/common/resolver/SystemTimeArgumentResolver.java b/src/main/java/me/hatter/sample/common/resolver/SystemTimeArgumentResolver.java new file mode 100644 index 0000000..6d9e505 --- /dev/null +++ b/src/main/java/me/hatter/sample/common/resolver/SystemTimeArgumentResolver.java @@ -0,0 +1,24 @@ +package me.hatter.sample.common.resolver; + +import me.hatter.sample.common.annotation.SystemTime; +import org.springframework.core.MethodParameter; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; + +import java.util.Date; + +@Component +public class SystemTimeArgumentResolver implements HandlerMethodArgumentResolver { + @Override + public boolean supportsParameter(MethodParameter parameter) { + return parameter.hasParameterAnnotation(SystemTime.class); + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + return new Date(); + } +} diff --git a/src/main/java/me/hatter/sample/web/controller/SampleController.java b/src/main/java/me/hatter/sample/web/controller/SampleController.java new file mode 100644 index 0000000..530b11d --- /dev/null +++ b/src/main/java/me/hatter/sample/web/controller/SampleController.java @@ -0,0 +1,17 @@ +package me.hatter.sample.web.controller; + +import me.hatter.sample.common.annotation.SystemTime; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Date; + +@RestController +public class SampleController { + + @GetMapping("/") + public String home( + @SystemTime Date time) { + return "Hello, World: " + time; + } +}