Created
August 27, 2015 17:07
-
-
Save dwelch2344/c173a49cc5c72a2fe76f to your computer and use it in GitHub Desktop.
Workaround to expose ids with SpringDataRest, if you can't extend RepositoryRestMvcConfiguration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@Configuration | |
public class SpringDataRestConfig { | |
private static final String BASE_PACKAGE = "com.example.yourapp"; | |
@Inject | |
private RepositoryRestConfiguration restConfig; | |
@Inject | |
private RepositoryRestMvcConfiguration restMvcConfig; | |
@PostConstruct | |
public void init(){ | |
ClassPathScanningCandidateComponentProvider scanner = | |
new ClassPathScanningCandidateComponentProvider(false); | |
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); | |
List<Class<?>> entities = scanner.findCandidateComponents(BASE_PACKAGE) | |
.parallelStream() | |
.map(bd -> parseClass(bd)) | |
.collect(Collectors.toList()); | |
restConfig.exposeIdsFor(entities.toArray(new Class<?>[entities.size()])); | |
} | |
private Class<?> parseClass(BeanDefinition bd) { | |
String name = bd.getBeanClassName(); | |
try { | |
return Class.forName(name); | |
} catch (ClassNotFoundException e) { | |
throw new RuntimeException("Could not parse bean name: " + name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice map and collect function :) Java 8?