Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created August 27, 2015 17:07
Show Gist options
  • Save dwelch2344/c173a49cc5c72a2fe76f to your computer and use it in GitHub Desktop.
Save dwelch2344/c173a49cc5c72a2fe76f to your computer and use it in GitHub Desktop.
Workaround to expose ids with SpringDataRest, if you can't extend RepositoryRestMvcConfiguration
@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);
}
}
}
@jakewilson801
Copy link

Nice map and collect function :) Java 8?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment