4343import org .springdoc .data .rest .core .DataRestRouterOperationService ;
4444
4545import org .springframework .context .ApplicationContext ;
46+ import org .springframework .core .annotation .AnnotatedElementUtils ;
4647import org .springframework .data .mapping .PersistentEntity ;
4748import org .springframework .data .mapping .PersistentProperty ;
4849import org .springframework .data .mapping .SimpleAssociationHandler ;
5455import org .springframework .data .rest .core .mapping .SearchResourceMappings ;
5556import org .springframework .data .rest .webmvc .BasePathAwareHandlerMapping ;
5657import org .springframework .data .rest .webmvc .ProfileController ;
58+ import org .springframework .data .rest .webmvc .RepositoryRestController ;
5759import org .springframework .data .rest .webmvc .RepositoryRestHandlerMapping ;
5860import org .springframework .data .rest .webmvc .alps .AlpsController ;
5961import org .springframework .data .rest .webmvc .json .JacksonMetadata ;
6062import org .springframework .data .rest .webmvc .mapping .Associations ;
6163import org .springframework .web .method .HandlerMethod ;
6264import org .springframework .web .servlet .HandlerMapping ;
6365import org .springframework .web .servlet .mvc .method .RequestMappingInfo ;
66+ import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
6467
6568/**
6669 * The type Spring repository rest resource provider.
6972public class SpringRepositoryRestResourceProvider implements RepositoryRestResourceProvider {
7073
7174 /**
72- * The constant REPOSITORY_ENTITY_CONTROLLER .
75+ * The constant SPRING_DATA_REST_PACKAGE .
7376 */
74- private static final String REPOSITORY_ENTITY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryEntityController " ;
77+ private static final String SPRING_DATA_REST_PACKAGE = "org.springframework.data.rest" ;
7578
7679 /**
77- * The constant REPOSITORY_SERACH_CONTROLLER .
80+ * The constant REPOSITORY_SCHEMA_CONTROLLER .
7881 */
79- private static final String REPOSITORY_SERACH_CONTROLLER = "org.springframework.data.rest. webmvc.RepositorySearchController " ;
82+ public static final String REPOSITORY_SCHEMA_CONTROLLER = SPRING_DATA_REST_PACKAGE + ". webmvc.RepositorySchemaController " ;
8083
8184 /**
82- * The constant REPOSITORY_SCHEMA_CONTROLLER .
85+ * The constant REPOSITORY_ENTITY_CONTROLLER .
8386 */
84- public static final String REPOSITORY_SCHEMA_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySchemaController" ;
87+ private static final String REPOSITORY_ENTITY_CONTROLLER = SPRING_DATA_REST_PACKAGE + ".webmvc.RepositoryEntityController" ;
88+
89+ /**
90+ * The constant REPOSITORY_SEARCH_CONTROLLER.
91+ */
92+ private static final String REPOSITORY_SERACH_CONTROLLER = SPRING_DATA_REST_PACKAGE + ".webmvc.RepositorySearchController" ;
8593
8694 /**
8795 * The constant REPOSITORY_PROPERTY_CONTROLLER.
8896 */
89- private static final String REPOSITORY_PROPERTY_CONTROLLER = "org.springframework.data.rest .webmvc.RepositoryPropertyReferenceController" ;
97+ private static final String REPOSITORY_PROPERTY_CONTROLLER = SPRING_DATA_REST_PACKAGE + " .webmvc.RepositoryPropertyReferenceController" ;
9098
9199 /**
92100 * The Delegating handler mapping class.
93101 */
94- private static final String DELEGATING_HANDLER_MAPPING_CLASS = "org.springframework.data.rest .webmvc.config.DelegatingHandlerMapping" ;
102+ private static final String DELEGATING_HANDLER_MAPPING_CLASS = SPRING_DATA_REST_PACKAGE + " .webmvc.config.DelegatingHandlerMapping" ;
95103
96104 /**
97105 * The Delegating handler mapping interface.
98106 */
99- private static final String DELEGATING_HANDLER_MAPPING_INTERFACE = "org.springframework.data.rest .webmvc.support.DelegatingHandlerMapping" ;
107+ private static final String DELEGATING_HANDLER_MAPPING_INTERFACE = SPRING_DATA_REST_PACKAGE + " .webmvc.support.DelegatingHandlerMapping" ;
100108
101109 /**
102110 * The constant LOGGER.
@@ -138,6 +146,11 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
138146 */
139147 private ApplicationContext applicationContext ;
140148
149+ /**
150+ * The Handler mapping list.
151+ */
152+ private List <HandlerMapping > handlerMappingList ;
153+
141154 /**
142155 * Instantiates a new Spring repository rest resource provider.
143156 *
@@ -159,9 +172,10 @@ public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositor
159172 this .mapper = mapper ;
160173 }
161174
175+
162176 public List <RouterOperation > getRouterOperations (OpenAPI openAPI ) {
163177 List <RouterOperation > routerOperationList = new ArrayList <>();
164- List < HandlerMapping > handlerMappingList = getHandlerMappingList ();
178+ handlerMappingList = getHandlerMappingList ();
165179 for (Class <?> domainType : repositories ) {
166180 Class <?> repository = repositories .getRequiredRepositoryInformation (domainType ).getRepositoryInterface ();
167181 DataRestRepository dataRestRepository = new DataRestRepository (domainType , repository );
@@ -223,34 +237,61 @@ else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
223237 return routerOperationList ;
224238 }
225239
240+ /**
241+ * Gets repository rest controller endpoints.
242+ *
243+ * @return the repository rest controller endpoints
244+ */
245+ @ Override
246+ public Map <String , Object > getRepositoryRestControllerEndpoints () {
247+ return applicationContext .getBeansWithAnnotation (RepositoryRestController .class );
248+ }
249+
250+ /**
251+ * Gets handler methods.
252+ *
253+ * @return the handler methods
254+ */
255+ @ Override
256+ public Map getHandlerMethods () {
257+ handlerMappingList = getHandlerMappingList ();
258+ return handlerMappingList .stream ().filter (RequestMappingInfoHandlerMapping .class ::isInstance )
259+ .flatMap (
260+ handler -> ((RequestMappingInfoHandlerMapping ) handler ).getHandlerMethods ().entrySet ().stream ())
261+ .filter (entry -> !entry .getValue ().getBeanType ().getName ().startsWith (SPRING_DATA_REST_PACKAGE ) && AnnotatedElementUtils .hasAnnotation (entry .getValue ().getBeanType (), RepositoryRestController .class ))
262+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
263+ }
226264 /**
227265 * Gets handler mapping list.
228266 *
229267 * @return the handler mapping list
230268 */
231269 private List <HandlerMapping > getHandlerMappingList () {
232- List <HandlerMapping > handlerMappingList = new ArrayList <>();
233- Class delegatingHandlerMappingClass = null ;
234- try {
235- delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_CLASS );
236- }
237- catch (ClassNotFoundException e ) {
270+ if (handlerMappingList == null ) {
271+ handlerMappingList = new ArrayList <>();
272+ Class delegatingHandlerMappingClass = null ;
238273 try {
239- delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_INTERFACE );
240- }
241- catch (ClassNotFoundException exception ) {
242- LOGGER .warn (e .getMessage ());
274+ delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_CLASS );
243275 }
244- }
245- if (delegatingHandlerMappingClass != null ) {
246- Object object = applicationContext .getBean (delegatingHandlerMappingClass );
247- try {
248- handlerMappingList = (List <HandlerMapping >) MethodUtils .invokeMethod (object , "getDelegates" );
276+ catch (ClassNotFoundException e ) {
277+ try {
278+ delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_INTERFACE );
279+ }
280+ catch (ClassNotFoundException exception ) {
281+ LOGGER .warn (e .getMessage ());
282+ }
249283 }
250- catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
251- LOGGER .warn (e .getMessage ());
284+ if (delegatingHandlerMappingClass != null ) {
285+ Object object = applicationContext .getBean (delegatingHandlerMappingClass );
286+ try {
287+ handlerMappingList = (List <HandlerMapping >) MethodUtils .invokeMethod (object , "getDelegates" );
288+ }
289+ catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
290+ LOGGER .warn (e .getMessage ());
291+ }
252292 }
253293 }
294+
254295 return handlerMappingList ;
255296 }
256297
@@ -312,8 +353,7 @@ private List<RouterOperation> findSearchControllers(List<RouterOperation> router
312353 * @param openAPI the open api
313354 * @return the list
314355 */
315- private List <RouterOperation > findControllers
316- (List <RouterOperation > routerOperationList ,
356+ private List <RouterOperation > findControllers (List <RouterOperation > routerOperationList ,
317357 Map <RequestMappingInfo , HandlerMethod > handlerMethodMap , ResourceMetadata resourceMetadata ,
318358 DataRestRepository dataRestRepository , OpenAPI openAPI ) {
319359 dataRestRouterOperationService .buildEntityRouterOperationList (routerOperationList , handlerMethodMap , resourceMetadata ,
0 commit comments