AEM 6.3 ResourceResolverFactory为null,并抛出NullPointerException

时间:2018-07-13 08:23:55

标签: aem osgi-bundle sling aem-6

也尝试过此帖子AEM 6.3 - ResourceResolverFactory is null in Service and throwing LoginException in Sling Model class

接口

package org.employee.employee.core.admin;

import org.apache.sling.api.resource.ResourceResolver;

public interface CustomerService {
    public String getCustomerData();
}

服务类==>获取resolverFactory null

package org.employee.employee.core.admin; 

import java.util.HashMap;
import java.util.Map;    
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype = true, immediate = true)
@Service
public class CustomerServiceImpl implements CustomerService {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    private ResourceResolverFactory resolverFactory;

    public String getCustomerData() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(ResourceResolverFactory.SUBSERVICE, "CustomerService");
        ResourceResolver resolver = null;
        try {
            resolver = resolverFactory.getServiceResourceResolver(param);
            System.out.println(resolver);
        } catch (LoginException loginExcp) {
            log.error("Exception while getting resource resolver." + loginExcp);
        }
        return resolver.toString();
    }
}

服务组件显示状态=>活动

enter image description here

UserMapper

enter image description here

2 个答案:

答案 0 :(得分:0)

我的访问方式就像

CustomerService customerService = new CustomerService();
String resolver = customerService.getCustomerData();

哪个是不正确的,并且由谁解决

@Reference
private CustomerService customerService;
String resolver = customerService.getCustomerData();

答案 1 :(得分:0)

我还相信,您不应使用管理员用户(如屏幕快照中所述)来访问服务,因为开发了服务映射是为了防止对存储库的无限制访问。

相反,创建一个新的系统用户,并仅向用户授予服务所需的那些权限。

要创建新的系统用户,您可以按照以下步骤操作:

  1. 在您的AEM实例上,打开http://server:port/crx/explorer/index.jsp
  2. 按屏幕左上角的“登录”链接,然后以管理员身份登录。
  3. 单击创建系统用户,并将用户ID分配为myServiceUser,并将中间路径分配为system / MyModule。
  4. 使用useradmin控制台向用户分配权限。
  5. 向ServiceUserMapper配置添加配置修订。

有关完整步骤,请参阅此帮助。 https://helpx.adobe.com/in/experience-manager/6-3/sites/administering/using/security-service-users.html

另一个SO答案证明了这一点: ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1

ACS Commons的此工具对于确保服务用户的存在非常有用。 https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html