From 74eedd1fef164b103e79a294b7957a5cc2a28f00 Mon Sep 17 00:00:00 2001 From: kimscott Date: Wed, 4 Sep 2019 11:24:48 +0900 Subject: [PATCH] =?UTF-8?q?id=20=EB=B3=B4=EC=9D=B4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/config/RepositoryRestConfig.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/com/example/template/config/RepositoryRestConfig.java diff --git a/src/main/java/com/example/template/config/RepositoryRestConfig.java b/src/main/java/com/example/template/config/RepositoryRestConfig.java new file mode 100644 index 0000000..dafd583 --- /dev/null +++ b/src/main/java/com/example/template/config/RepositoryRestConfig.java @@ -0,0 +1,35 @@ +package com.example.template.config; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.filter.RegexPatternTypeFilter; +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; + +import java.util.Set; +import java.util.regex.Pattern; + +@Configuration +public class RepositoryRestConfig implements RepositoryRestConfigurer { + + @Override + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { + + final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); + provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*"))); + + final Set beans = provider.findCandidateComponents("com.example"); + + for (BeanDefinition bean : beans) { + Class idExposedClasses = null; + try { + idExposedClasses = Class.forName(bean.getBeanClassName()); + config.exposeIdsFor(Class.forName(idExposedClasses.getName())); + } catch (ClassNotFoundException e) { + // Can't throw ClassNotFoundException due to the method signature. Need to cast it + throw new RuntimeException("Failed to expose `id` field due to", e); + } + } + } +} \ No newline at end of file