SimpleUrlHandlerMapping is causing double call of HandlerInterceptor. This is related to https://github.com/spring-projects/spring-data-rest/issues/1955 which has been fixed; should be an easy fix.
This bug was recently introduced in the last year and was fixed in other mappings but not underlying ones like SimpleUrlHandlerMapping.
I have tested and have working code showing an example (but requires external properties/config files).
Have tried removing filters, different adapters. Still the same.
Wasn't until I saw that error that I noticed the bug wasn't fixed in SimpleUrlHandlerMapping
.
Output from a debug more clearly shows the error:
11:26:34.378 [http-nio-8080-exec-1] DEBUG o.s.security.web.FilterChainProxy - Secured GET /v0.4/user/show/5
Parsing Uri.......
11:26:34.397 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - GET "/v0.4/user/show/5", parameters={}
11:26:34.404 [http-nio-8080-exec-1] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapped to demo.application.controller.UserController@40d10481
interceptor
NO CACHED API RESPONSE
interceptor
NO CACHED API RESPONSE
at usercontroller...
user/show
has user...
[id, version, username, password, uid, firstName, lastName, email, emailVerified, oauthId, oauthProvider, avatarUrl, enabled, accountExpired, accountLocked, passwordExpired, authorities, userame]
[id, version, username, password, uid, firstName, lastName, email, emailVerified, oauthId, oauthProvider, avatarUrl, enabled, accountExpired, accountLocked, passwordExpired, authorities, userame]
The 'UserController' is only called once (as shown above) but the handlerinterceptor's prehandle/posthandle are being triggered twice each
Comment From: orubel
Checked the class and just like the original issue noted above, it extends 'AbstractUrlHandlerMapping' and isn't implementing 'Ordered'
*/
public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
private final Map<String, Object> urlMap = new LinkedHashMap<>();
/**
Quote from other patch:
"So when doing the post request i noticed that on the branch with the old version Class: AbstractHandlerMapping Method: public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { Line: HandlerExecutionChain executionChain = this.getHandlerExecutionChain(handler, request); is called once and afterwards the interceptors are added to the list in Class: HandlerExecutionChain in the method public void addInterceptor(HandlerInterceptor interceptor) { this.interceptorList.add(interceptor); }"
Comment From: orubel
It looks like most mapping have moved away from AbstractHandlerMapping (which AbstractUrlHandlerMapping extends) and are now using DelegateHandlerMapping) - https://github.com/spring-projects/spring-data-rest/commit/4d7daa9f6e0b2edbf526b35fa330be392e67507b
Comment From: orubel
SOLVED. Issue is with setting initApplicationContext()
' in a starter
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.registerHandlers(urlMap)
mapping.setUrlMap(urlMap);
//mapping.setOrder(1);
mapping.setInterceptors(new Object[]{ new ApiInterceptor(principleService, exchangeService, apiCacheService, apiProperties) })
mapping.setApplicationContext(context);
//mapping.initApplicationContext()
Commented it out (like above) and issue resolved itself
Closing with smack to head