Skip to content

Instantly share code, notes, and snippets.

@arahansa
Created March 2, 2017 11:27
Show Gist options
  • Save arahansa/4b9145e8f508f7f3b80fec66acd253ee to your computer and use it in GitHub Desktop.
Save arahansa/4b9145e8f508f7f3b80fec66acd253ee to your computer and use it in GitHub Desktop.
스프링 컨트롤러 연습
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ComApi {
String path() default "/com";
}
@RequestMapping("/arahansa")
@RestController
static class TestController {
List<String> list = Arrays.asList("test,1,2,3,4,5,6,7".split(","));
// 접근가능한 주소 /arahansa/test
@GetMapping("/test")
public List<String> apiList() {
return list;
}
// 접근 가능한 주소 /com/arahansa/test2, /arahansa/test2
@ComApi
@RequestMapping(value = "/test2", method = {RequestMethod.GET, RequestMethod.POST})
public List<String> test2() {
return list;
}
}
@Configuration
static class MvcConfig extends WebMvcConfigurationSupport {
@Bean
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new RequestMappingHandlerMapping(){
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo info = createRequestMappingInfo(method);
if (info != null) {
RequestMappingInfo typeInfo = this.createRequestMappingInfo(handlerType);
if (typeInfo != null) {
info = typeInfo.combine(info);
}
ComApi comApi = AnnotatedElementUtils.findMergedAnnotation(method, ComApi.class);
if (comApi != null) {
// RequestMappingInfo 두번째 생성자 인자에
// new PatternsRequestCondition(comApi.path(), "")
RequestMappingInfo info2 = new RequestMappingInfo(null, new PatternsRequestCondition(comApi.path(), ""), null, null, null, null, null, null);
RequestMappingInfo combine = info2.combine(info);
log.info("after combine webapi : {}", combine);
return combine;
}
}
return info;
}
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
RequestCondition condition = element instanceof Class ? this.getCustomTypeCondition((Class) element) : this.getCustomMethodCondition((Method) element);
return requestMapping != null ? this.createRequestMappingInfo(requestMapping, condition) : null;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment