我对很多类都做了完全相同的事情,但它总是会返回我可以迭代并在需要时测试 instanceof 的基本类型。您可能必须将对象强制转换为子类。如果您不添加@ExtraTypes,您将知道,因为在服务器端您将收到一条消息,指出无法将 MoreSpecificChildBean 发送到客户端。
我只注释服务而不是代理,我在 2.4 中遇到了一些怪癖,将 @ExtraTypes 添加到代理。
/**
* Base proxy that all other metric proxies extend. It is used mainly for it's
* inheritence with the RequestFactory. It's concrete implementation is
* {@link MetricNumber}.
*
* @author chinshaw
*/
@ProxyFor(value = Metric.class, locator = IMetricEntityLocator.class)
public interface MetricProxy extends DatastoreObjectProxy {
/**
* Name of this object in the ui. This will commonly be extended by
* subclasses.
*/
public String NAME = "Generic Metric";
/**
* This is a list of types of outputs that the ui can support. This is
* typically used for listing types of supported Metrics in the operation
* output screen.
*
* @author chinshaw
*/
public enum MetricOutputType {
MetricNumber, MetricString, MetricCollection, MetricStaticChart, MetricDynamicChart
}
/**
* See {@link MetricNumber#setName(String)}
*
* @param name
*/
public void setName(String name);
/**
* See {@link MetricNumber#setContext(String)}
*
* @return name of the metric.
*/
public String getName();
/**
* Get the list of violations attached to this metric.
*
* @return
*/
public List<ViolationProxy> getViolations();
}
@ProxyFor(value = MetricNumber.class, locator = IMetricEntityLocator.class)
public interface MetricNumberProxy extends MetricProxy {
public List<NumberRangeProxy> getRanges();
public void setRanges(List<NumberRangeProxy> ranges);
}
...
@ProxyFor(value = MetricDouble.class, locator = IMetricEntityLocator.class)
public interface MetricDoubleProxy extends MetricNumberProxy {
/* Properties when fetching the object for with clause */
public static String[] PROPERTIES = {"ranges"};
public Double getValue();
}
...
@ProxyFor(value = MetricPlot.class, locator = IMetricEntityLocator.class)
public interface MetricPlotProxy extends MetricProxy {
/**
* UI Name of the object.
*/
public String NAME = "Static Plot";
public String getPlotUrl();
}
这是一个组合方法,因为我通常总是返回可能包含指标列表的复合类。话虽如此,这将返回我基本类型的指标,然后我可以转换它们。
@ExtraTypes({ MetricProxy.class, MetricNumberProxy.class, MetricDoubleProxy.class, MetricIntegerProxy.class})
@Service(value = AnalyticsOperationDao.class, locator = DaoServiceLocator.class)
public interface AnalyticsOperationRequest extends DaoRequest<AnalyticsOperationProxy> {
Request<List<<MetricProxy>> getSomeMetrics();
}
这不是我使用的确切方法,但可以用于获取类型代理。
context.getSomeMetrics().with(MetricNumber.PROPERTIES).fire(new Receiver<List<MetricProxy>>() {
public void onSuccess(List<MetricProxy> metrics) {
for (MetricProxy metric : metrics) {
if (metric instanceof MetricDoubleProxy) {
logger.info("Got a class of double " + metric.getValue());
}
}
}
}
当您收到上述错误时,您将知道是否缺少 @ExtraTypes 注释。
希望有帮助