服务组件层的扩展

发送反馈


服务组件层的实现形式如下:

@Component(providerTypes={MapProvider.class},optional=false, type = "PushpinMap")
public class PushpinComponent implements ComponentContextAware{
        ...
}

@Component 注记标识了该服务组件的一些元信息,详细说明,请参见 Javadoc:com.supermap.services.components.Component

其中:

服务组件上下文

获取服务组件配置

获取服务组件

服务组件扩展示例

服务组件上下文

在服务组件实现类中,可以通过 ComponentContextAware 接口获取服务组件上下文,通过服务组件上下文,可以获取服务提供者、服务组件配置信息。

注意:为提高灵活性,并使领域服务的结构更加清晰,我们不推荐将业务逻辑直接放在服务组件层,推荐放在服务提供者层,服务组件通过服务提供者实现业务功能。

获取服务组件配置

用户自定义服务服务配置信息在 iserver-services.xml 中(详见iServer 配置文件说明)。服务组件配置信息的结构由服务组件配置类确定,服务组件配置如下:

<component class="com.supermap.sample.SampleComponent" interfacenames="rest" name="samplecomponent" providers="ugcMapProvider-China400">
        <config class="com.supermap.sample.SampleComponentConfig">
                <param1>default</param1>
                ...
        </config>
</component>

其中,SampleComponentConfig 是 SampleComponent 对应的配置类,param1对应 SampleComponentConfig 类的属性。“rest”是该服务组件用来发布 Web 服务的服务接口名,“ugcMapProvider-China400”是该服务组件使用的服务提供者名(参见:服务配置文件结构)。

在服务组件实现类中,可以通过 ComponentContextAware 接口获取服务组件上下文,通过服务组件上下文,可以获取服务组件配置信息(即以上配置),具体方式如下:

@Component(providerTypes={MapProvider.class},optional=false, type = "SampleType")
public class SampleComponent implements ComponentContextAware{
        ...
        public void setComponentContext(ComponentContext context) {
                SampleComponentConfig config=context.getConfig(SampleComponentConfig.class);
                ...
        }
}

SampleComponentConfig  对象即对应配置信息中的<config class="com.supermap.sample.SampleComponentConfig"/> 配置项。

获取服务组件

SuperMap iServer 中,服务组件可通过服务组件上下文获取服务提供者,从而获取相应的 GIS 功能。获取方式如下:

@Component(providerTypes={MapProvider.class},optional=false, type = "SampleType")
public class SampleComponent implements ComponentContextAware{
        ...
        public void setComponentContext(ComponentContext context) {
                List<MapProvider> mapproviders=context.getProviders(MapProvider.class);
                ...
        }
}

服务组件扩展示例

扩展服务组件,添加新的服务组件,能重用现有的服务提供者(1个或多个)实现更丰富的功能。

这里实现一个新的服务组件 PushpinComponent,使用地图服务提供者,提供在地图上进行标注的功能。

PushpinComponent 的实现代码如下:

PushpinComponent.java

public String GetImage(double pushpinX,double pushpinY) 函数用于返回一个地图图片 URI,地图的(pushpinX,pushpinY)位置用红旗符号标记。

注意:图片 URI 中包含服务器 IP 和端口,本示例中,采用将服务组件快速发布为 REST 资源的方式,对图片 URI 中的 IP 和端口号,需要在服务组件中处理,这里替换为 localhost 和 8090,如下:

                imageURI=imageURI.replace("{ip}", "localhost");
                imageURI=imageURI.replace("{port}", "8090");

该服务组件的配置类实现如下:

PushpinCofig.java

编译后,将 Jar 包放到 %SuperMap iServer_HOME%\webapps\iserver\WEB-INF\lib 目录下。

作为示例,PushpinComponent 使用 China.smwu 工作空间中的“China”出图,使用 iServer 默认配置的 ugcProvider-China400服务提供者,在 iserver-services.xml 中配置如下:

<component class="com.supermap.sample.component.PushpinComponent" interfaceNames="rest" name="map-pushpin" providers="ugcMapProvider-China400">
        <config class="com.supermap.sample.component.PushpinCofig">
                <mapName>China</mapName>
        </config>
</component>

interfaceNames="rest" 标明把该组件发布为 REST 服务,SuperMap iServer 可通过 Restlet 发布机制,将任意一个服务组件快速发布为 REST 资源(参见:发布领域服务组件为 REST 资源)。

在 http://localhost:8090/iserver/services/map-pushpin/rest/domainComponents/PushpinComponent 页面,可以看到 PushpinComponent 的方法映射为的资源列表,点击 GetImageResult 资源,通过 html 表述输入参数,如 13000000.0,4800000.0,获取 xml 表述;或直接访问如下 URI:

http://localhost:8090/iserver/services/map-pushpin/rest/domainComponents/PushpinComponent/GetImageResult.xml?arg0=13000000&arg1=4800000

获取 XML 表述如下:

<string>
http://localhost:8090//iserver/output/temp/China_1f4xfa/30236220/10x7/4129x3129_89252943.png?_t=1540880753569
</string>

即为结果地图的访问地址,访问图片 URI,获取地图图片如下所示: