AbdulazizYas
AbdulazizYas

Reputation: 1

Is there a way to change the URL endpoint of @RequestMapping based on annotation in Spring Boot?

I want to change the endpoint of a @RequestMapping in Spring Boot based on specific annotation

For example:

@Version(1)
@GetMapping("/test")
public ResponseEntity test(){
   return new ResponseEntity();
}

this should product the endpoint: /v1/test

I tried extending the RequestMappingHandlerMapping and overriding getMappingForMethod(Method method, Class<?> handlerType) method

@Component
public class VersionedRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        RequestMappingInfo methodMapping = super.getMappingForMethod(method, handlerType);
        if (methodMapping != null) {
            Version versionAnnotation = AnnotationUtils.findAnnotation(handlerType, Version.class);
            if (versionAnnotation != null) {
                String versionPath = "/v" + versionAnnotation.value();
                RequestMappingInfo typeMapping = RequestMappingInfo.paths(versionPath).build();
                return typeMapping.combine(methodMapping);
            }
        }
        return methodMapping;
    }
}

but id did not work

Any solution for this problem?

Upvotes: 0

Views: 163

Answers (0)

Related Questions