You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ICM 7.10.38.11-LTS中REST资源类依赖注入失效求助

Intershop REST Cartridge依赖注入失败(LocaleMgr/CurrentApplicationBOProvider注入问题)

问题根源

Intershop REST资源依赖HK2容器实现依赖注入,你的配置存在两个核心问题:

  1. implementations.component未声明对LocaleMgr、CurrentApplicationBOProvider这类依赖的需求,容器无法识别需要注入的对象
  2. instances.component未关联Intershop提供的全局依赖实例,导致容器找不到可注入的对象

解决方案

1. 更新implementations.component,添加依赖声明

在WarehouseResource的实现配置中,补充需要注入的依赖契约:

<components xmlns="http://www.intershop.de/component/2010">
    <implementation name="WarehouseResource" implements="AbstractRestResource" class="com.luminoslabs.warehouse.rest.capi.WarehouseResource">
        <requires name="subResource" contract="RestResource" cardinality="0..n" />
        <requires name="name" contract="String" cardinality="1..1" />
        <!-- 新增需要注入的依赖声明 -->
        <requires name="localeMgr" contract="LocaleMgr" cardinality="1..1" />
        <requires name="currentApplicationBOProvider" contract="CurrentApplicationBOProvider" cardinality="1..1" />
    </implementation>
</components>

2. 修改instances.component,关联全局依赖实例

在实例化WarehouseResource时,注入Intershop提供的现成组件实例:

<components xmlns="http://www.intershop.de/component/2010"> 
    <!-- Way where you attach your new resource to existing REST API root -->
    <fulfill requirement="subResource" of="intershop.B2BWebShop.RESTAPI.root">
        <instance name="intershop.B2BWebShop.RESTAPI.WarehouseResource" with="WarehouseResource">
            <fulfill requirement="name" value="warehouses" />
            <!-- 关联LocaleMgr全局实例 -->
            <fulfill requirement="localeMgr" ref="LocaleMgr" />
            <!-- 关联CurrentApplicationBOProvider全局实例 -->
            <fulfill requirement="currentApplicationBOProvider" ref="CurrentApplicationBOProvider" />
        </instance>
    </fulfill>
    <fulfill requirement="resourceACLCartridge" value="app_sf_rest_warehouse" of="intershop.B2BWebShop.RESTAPI.AuthorizationService"/>
</components>

3. 调整Java代码,移除手动注入逻辑

删除构造函数中的NamingMgr相关代码,依赖容器自动注入:

@Tag(name = "Warehouse")
@OpenAPIDefinition( info = @Info(version = "1.0.0"))
public class WarehouseResource extends AbstractRestResource implements RestResource
{
    @Inject
    private CurrentApplicationBOProvider currentApplicationBOProvider;
    
    @Inject
    private LocaleMgr localeMgr;
    
    public WarehouseResource()
    {
        super();
        // 移除所有手动注入代码
    }
    
    @Operation(
                    summary = "Returns the list of warehouses."
                 )
                 @ApiResponses({@ApiResponse(
                 responseCode = "200",
                 description = "The list of warehouses.",
                 content = {@Content(
                 schema = @Schema(
                 implementation = Collection.class
              )
              )}
              ), @ApiResponse(
                 responseCode = "401",
                 ref = "401"
              )})
    @GET
    @Produces({"application/json"})
    public Response getWarehouses()
    {
        final WarehouseBORepository warehouseBORepository = currentApplicationBOProvider.get().getRepository(WarehouseBORepository.EXTENSION_ID);
        final Collection<WarehouseBO> warehouseBOs = warehouseBORepository.getAllWarehouseBOs();
        final Collection<WarehouseRO> warehouseROs = Optional.ofNullable(warehouseBOs)
            .map(warehouses -> warehouses.stream()
                .map(WarehouseRO::new)
                .collect(Collectors.toList()))
            .orElse(new ArrayList<>(0));
        
        return Response.ok(warehouseROs).build();
    }
}

4. 重新部署验证

  • 清理项目构建目录,重新编译cartridge
  • 部署更新后的app_sf_rest_warehouse cartridge
  • 重启Intershop应用服务器后,测试REST接口

内容的提问来源于stack exchange,提问作者mihallievich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:00:48