init commit
This commit is contained in:
28
src/main/java/me/hatter/sample/Application.java
Normal file
28
src/main/java/me/hatter/sample/Application.java
Normal file
@@ -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);
|
||||
// }
|
||||
//
|
||||
// };
|
||||
// }
|
||||
}
|
||||
21
src/main/java/me/hatter/sample/common/WebConfig.java
Normal file
21
src/main/java/me/hatter/sample/common/WebConfig.java
Normal file
@@ -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<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(systemTimeArgumentResolver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package me.hatter.sample.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface SystemTime {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user