It seems to be done like this (please correct me if this solution has any drawbacks and I'll happily accept you answer)
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
public class MyPrefixedRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
if (mappingInfo == null) {
return null;
}
MyAnnnotationForPrefix myAnnotation = handlerType.getAnnotation(MyAnnnotationForPrefix.class);
if (myAnnotation == null) {
return mappingInfo;
}
PatternsRequestCondition patternsRequestCondition =
new PatternsRequestCondition(myAnnotation.getValue())
.combine(mappingInfo.getPatternsCondition());
return new RequestMappingInfo(mappingInfo.getName(),
patternsRequestCondition,
mappingInfo.getMethodsCondition(),
mappingInfo.getParamsCondition(),
mappingInfo.getHeadersCondition(),
mappingInfo.getConsumesCondition(),
mappingInfo.getProducesCondition(),
mappingInfo.getCustomCondition()
);
}
}
Also you need add this RequestMappingHandlerMapping to you webmvc config. In spring-boot this is done, defining bean:
@Component
public class MyPrefixedWebMvcRegistrations extends WebMvcRegistrationsAdapter {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new MyPrefixedRequestMappingHandlerMapping();
}
}