user19570102
user19570102

Reputation: 9

How to register resourceResolverFactory with custom resource mapping in aem context?

I have a model class where i inject ResourceresolverFactory and obtain resourceResolver using it to map my link urls. resourceResolver = resourceResolverFactory.getServiceResourceresolver(map); resourceResolver.map(link);

I am using aemContext to test this class and want to configure the resourceResolverFactory of aemContext with custom resource mapping.

I tried the below snippet of code however it gave me an error.I am not sure how else to configure custom configuration and register the service with aem context

@BeforeEach
public void setUp(AemContext context) {
    // Define custom mappings for the ResourceResolverFactory
    Map<String, Object> resolverConfig = new HashMap<>();
    resolverConfig.put("resource.resolver.mapping", new String[]{
        "/content/:/",
        "/content/dam/:/dam",
        "/etc/map/:/mymap"
    });

    // Register the ResourceResolverFactory with the custom mappings
    context.registerInjectActivateService(ResourceResolverFactory.class, resolverConfig);

    // Optionally, load some test content into the context
    context.load().json("/path/to/test-content.json", "/content");
}

I obtain the following error while trying to use this configuration and running my code

Error image

Upvotes: 0

Views: 83

Answers (1)

Raphael Schweikert
Raphael Schweikert

Reputation: 18556

It’s not possible to instantiate a service from ResourceResolverFactory.class because ResourceResolverFactory is an interface.

There is a constructor for AemContext that takes a map of resource resolver factory props as argument. Try passing your resolverConfig to that.

Upvotes: 0

Related Questions