从Java自定义编组通过BlazeDS的到Flex [英] Custom Marshalling from Java to Flex via BlazeDS
问题描述
我的团队正在组建了概念证明的Flex应用程序坐在使用BlazeDS的基于Spring的服务器之上。
My team are putting together a proof-of-concept Flex application sitting on top of a Spring-based server using BlazeDS.
我们做了很多的日期计算的,所以我们使用约达时间广泛遍布code和在我们的领域模型。
We do quite a lot of date calculations, so we use Joda Time extensively throughout the code and in our domain model.
我们现在试图找出我们如何可以继续使用约达时间在我们的DTO是通过BlazeDS的发回的往复与Flex前端。
We're now trying to figure out how we can continue to use Joda Time in our DTOs that are sent back-and-forth with the Flex frontend via BlazeDS.
我们的目标是使用ActionScript 3数据类型日期
在Flex端并映射到我们使用的乔达时间的的DateTime 对code>,
LocalDate
和本地时间
在Java端类型。
Our goal is to use the Actionscript 3 data type Date
on the Flex side and have that map to our use of Joda time's DateTime
, LocalDate
and LocalTime
types on the Java side.
我们能够解决转换的ActionScript 3的日期的问题
类型使用自定义类型编组插入到BlazeDS的调用Java的时候,但这似乎只为Flex-调用>的Java / BlazeDS的方向,而不是为Java / BlazeDS-> Flex的方向。
We can solve the problem of converting Actionscript 3's Date
type when calling Java with a custom type marshaller plugged into BlazeDS, but this appears to only be invoked for the Flex->Java/BlazeDS direction and not for the Java/BlazeDS->Flex direction.
我现在在看的自定义 PropertyProxy
的实现BlazeDS的,但是这并不像正确的事,无论是。
I'm now looking at custom PropertyProxy
implementations for BlazeDS, but this doesn't look like the right thing either.
另一个想法是实施外部化
在我们的Java DTO的,但是这似乎有太多的工作,尤其是当我看到BlazeDS的对手GraniteDS的,并显示插入乔达时间支持他们用一个简单的类型转换器的文档!
The other idea was to implement Externalizable
on our Java DTOs, but this seems like too much work, especially when I look at the BlazeDS rival GraniteDS and that shows plugging in Joda Time support in their documentation with a simple type converter!
任何想法AP preciated。
Any ideas appreciated.
推荐答案
确定 - 我发现我自己的答案。这涉及编写自己的AMF端点类+相关序列化类。我得说,男人在上 http://flexblog.faratasystems.com 已经在黑客BlazeDS的灵感的重要来源。
OK - I've found the answer on my own. This involved writing my own AMF endpoint class + related serializing classes. I've gotta say that the guys over at http://flexblog.faratasystems.com have been a great source of inspiration on hacking BlazeDS.
这code应该被纳入到BlazeDS的本身还是一些开源扩建工程 - 它是如此基本
This code should really be incorporated into BlazeDS itself or some Open Source extension project - it's so basic.
<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>
自定义AMF端点
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);
}
}
自定义AMF 0处理
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);
}
}
自定义AMF 3处理
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();
}
}
自定义的Marshaller的Flex->的Java调用
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);
}
}
这篇关于从Java自定义编组通过BlazeDS的到Flex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!