【问题标题】:AEM 6.3 ResourceResolverFactory is null and throwing NullPointerExceptionAEM 6.3 ResourceResolverFactory 为空并抛出 NullPointerException
【发布时间】:2018-07-13 08:23:55
【问题描述】:

也试过这个帖子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();
}

Service Class ==> 获取 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();
    }
}

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

用户映射器

【问题讨论】:

  • 您如何访问客户服务?
  • 解决了。我访问错了。
  • 如果通过OSGi容器访问,OSGi将注入依赖,否则,它们将为空。我想最初你只是创建类而不使用.getService(_interface_)。
  • @ruchikajain - 请添加您的解决方案作为答案,以便其他人从中受益。

标签: aem osgi-bundle sling aem-6


【解决方案1】:

我访问它就像

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

不正确的解决方法

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

【讨论】:

    【解决方案2】:

    我也相信你不应该使用管理员用户(如屏幕截图中提到的)来访问服务,因为服务映射是为了防止对存储库的无限制访问而开发的。

    而是创建一个新的系统用户,并仅授予用户服务所需的权限。

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

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

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

    另一个证明这一点的答案: 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

    【讨论】:

      猜你喜欢
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      相关资源
      最近更新 更多