通过BlazeDS从Java到Flex的自定义编组
我的团队正在使用BlazeDS在基于Spring的服务器上构建一个概念验证的Flex应用程序 我们做了大量的日期计算,所以我们在整个代码和领域模型中广泛使用Joda Time 我们现在正试图弄清楚如何在通过BlazeDS通过Flex前端来回发送的DTO中继续使用Joda Time 我们的目标是在Flex端使用Actionscript 3数据类型通过BlazeDS从Java到Flex的自定义编组,java,apache-flex,actionscript-3,blazeds,jodatime,Java,Apache Flex,Actionscript 3,Blazeds,Jodatime,我的团队正在使用BlazeDS在基于Spring的服务器上构建一个概念验证的Flex应用程序 我们做了大量的日期计算,所以我们在整个代码和领域模型中广泛使用Joda Time 我们现在正试图弄清楚如何在通过BlazeDS通过Flex前端来回发送的DTO中继续使用Joda Time 我们的目标是在Flex端使用Actionscript 3数据类型Date,并将其映射到Java端使用Joda time的DateTime、LocalDate和LocalTime类型 当使用插入BlazeDS的自定义类型
Date
,并将其映射到Java端使用Joda time的DateTime
、LocalDate
和LocalTime
类型
当使用插入BlazeDS的自定义类型封送器调用Java时,我们可以解决转换ActionScript3的Date
类型的问题,但这似乎只适用于Flex->Java/BlazeDS方向,而不适用于Java/BlazeDS->Flex方向
我现在正在研究BlazeDS的定制PropertyProxy
实现,但这看起来也不太合适
另一个想法是在我们的Java DTO上实现Externalizable
,但这似乎是太多的工作了,特别是当我看到BlazeDS的竞争对手GraniteDS时,这显示了在他们的文档中使用简单的类型转换器插入Joda Time支持
任何想法都值得赞赏。您是否尝试过本博客中概述的定制封送员方法:
好的-我自己找到了答案。这涉及到编写我自己的AMF端点类+相关的序列化类。我必须说,在那里的人一直是黑客BlazeDS的灵感源泉 这段代码真的应该整合到BlazeDS本身或一些开源扩展项目中——它太基本了 频道定义 自定义序列化程序 自定义AMF 0处理 自定义AMF 3处理 用于Flex->Java调用的自定义封送拆收器
对于使用SpringSource的Spring BlazeDS集成项目的Java应用程序,有一种更简单的处理方法:
- 编写GenericConverter的实现,该实现处理ReadableDateTime与java.util.Date之间的映射
- 创建AbstractAmfConversionServiceConfigProcessor的子类并覆盖configureConverters,将转换器实现添加到注册表中
- 通过创建ConfigProcessor的实例并将其连接起来,更新Spring配置:
更多信息请点击此处:
此编组用于Actionscript->Java方向,这是我在调用Java时使用的,但它不用于编组服务调用的结果。这看起来值得研究。我们已经添加了一个BigDecimal实现,并提供了我们需要编写的代码(低级的东西),如果这将插入Springs impl,我会感到惊讶。如果您有任何问题,请随时在Twitter上给我发消息。我很惊讶这是多么容易实现。
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="ch.hedgesphere.core.blazeds.endpoint.AMFEndpoint"/>
<properties>
<serialization>
<type-marshaller>ch.hedgesphere.core.blazeds.translator.HedgesphereASTranslator</type-marshaller>
</serialization>
</properties>
</channel-definition>
package ch.hedgesphere.core.blazeds.endpoint;
import ch.hedgesphere.core.blazeds.serialization.Serializer;
public class AMFEndpoint extends flex.messaging.endpoints.AMFEndpoint {
@Override
protected String getSerializerClassName() {
return Serializer.class.getName();
}
}
package ch.hedgesphere.core.blazeds.serialization;
import java.io.OutputStream;
import flex.messaging.io.MessageIOConstants;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.AmfMessageSerializer;
import flex.messaging.io.amf.AmfTrace;
public class Serializer extends AmfMessageSerializer {
@Override
public void initialize(SerializationContext context, OutputStream out, AmfTrace trace)
{
amfOut = new AMF0Output(context);
amfOut.setOutputStream(out);
amfOut.setAvmPlus(version >= MessageIOConstants.AMF3);
debugTrace = trace;
isDebug = trace != null;
amfOut.setDebugTrace(debugTrace);
}
}
package ch.hedgesphere.core.blazeds.serialization;
import flex.messaging.io.SerializationContext;
public class AMF0Output extends flex.messaging.io.amf.Amf0Output {
public AMF0Output(SerializationContext context) {
super(context);
}
@Override
protected void createAMF3Output()
{
avmPlusOutput = new AMF3Output(context);
avmPlusOutput.setOutputStream(out);
avmPlusOutput.setDebugTrace(trace);
}
}
package ch.hedgesphere.core.blazeds.serialization;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import flex.messaging.io.SerializationContext;
public class AMF3Output extends flex.messaging.io.amf.Amf3Output {
public AMF3Output(SerializationContext context) {
super(context);
}
@Override
public void writeObject(Object value) throws IOException {
if(value instanceof DateTime) {
value = convertToDate((DateTime)value);
}
if(value instanceof LocalDate) {
value = convertToDate((LocalDate)value);
}
if(value instanceof LocalTime) {
value = convertToDate((LocalTime)value);
}
super.writeObject(value);
}
private Object convertToDate(LocalTime time) {
return time.toDateTimeToday().toDate();
}
private Object convertToDate(LocalDate date) {
return date.toDateMidnight().toDate();
}
private Object convertToDate(DateTime dateTime) {
return dateTime.toDate();
}
}
package ch.hedgesphere.core.blazeds.translator;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import flex.messaging.io.amf.translator.ASTranslator;
public class HedgesphereASTranslator extends ASTranslator {
@SuppressWarnings({"rawtypes"})
@Override
public Object convert(Object originalValue, Class type) {
if( type.equals(DateTime.class)) {
return convertToDateTime(originalValue);
}
if( type.equals(LocalDate.class)) {
return convertToLocalDate(originalValue);
}
if( type.equals(LocalTime.class)) {
return convertToLocalTime(originalValue);
}
return super.convert(originalValue, type);
}
private Object convertToLocalTime(Object originalValue) {
return originalValue == null ? null : new LocalTime(originalValue);
}
private Object convertToLocalDate(Object originalValue) {
return originalValue == null ? null : new LocalDate(originalValue);
}
private Object convertToDateTime(Object originalValue) {
return originalValue == null ? null : new DateTime(originalValue);
}
@SuppressWarnings({"rawtypes"})
@Override
public Object createInstance(Object source, Class type) {
return super.createInstance(source, type);
}
}
<flex:message-broker>
<flex:config-processor ref="customConfigProcessor"/>
</flex:message-broker>