Smooks:从Java输出EDI [英] Smooks: Outputting EDI from Java

查看:767
本文介绍了Smooks:从Java输出EDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Smooks EDI writer 中提出的解决方案的启发,具有以下Java代码:

Inspired by the proposed solution in Smooks EDI writer, I have the following Java code:

// Creates minimal EDI message with one field populated
EdifactV3D98AMEDPID edi = new EdifactV3D98AMEDPID();
UNBInterchangeHeader header = new UNBInterchangeHeader();
UNBInterchangeHeader.S002SenderIdentification s002SenderIdentification = new UNBInterchangeHeader.S002SenderIdentification();
s002SenderIdentification.setE0004SenderIdentification("TEST");
header.setS002SenderIdentification(s002SenderIdentification);
edi.setUNBInterchangeHeader(header);

Smooks smooks = new Smooks("edi-output-smooks-config.xml");

// Sets up access to exports specified in Smooks config
ExecutionContext executionContext = smooks.createExecutionContext();
Exports exports = Exports.getExports(smooks.getApplicationContext());
Result[] results = exports.createResults();

smooks.filterSource(executionContext, new JavaSource(edi), results);

List<Object> objects = Exports.extractResults(results, exports);
JavaResult.ResultMap map = (JavaResult.ResultMap) objects.get(0);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();
UNEdifactInterchange41 unEdifactInterchange = (UNEdifactInterchange41) map.get("unEdifactInterchange");

// Should output EDI message as String, but StringWriter is empty
StringWriter ediOutStream = new StringWriter();
factory.toUNEdifact(unEdifactInterchange, ediOutStream);

...具有以下Smooks配置:

... with the following Smooks config:

<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                  xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

    <!-- Imports D98A Edifact binding found in Maven dependency -->
    <import file="/org/milyn/edi/unedifact/d98a/message-bindingconfig.xml" />
    <import file="/org/milyn/smooks/edi/unedifact/model/r41/bindings/unedifact-interchange.xml" />

    <!-- Configures Result object for accessing EDI output -->
    <core:exports>
        <core:result type="org.milyn.payload.JavaResult"/>
    </core:exports>

</smooks-resource-list>

StringWriter返回一个空字符串,而我希望/期望将Java对象作为EDI字符串.

The StringWriter returns an empty String, whereas I had hoped/expected the Java object as an EDI String.

有什么建议或建议吗?

推荐答案

我找不到EdifactV3D98AMEDPID,UNBInterchangeHeade类.但是我做了类似的测试:

I don't find the classes EdifactV3D98AMEDPID, UNBInterchangeHeade. But i made a similar test:

UNEdifactInterchange41 edi = new UNEdifactInterchange41();
UNB41 header = new UNB41();
header.setSender(null);
Party sender = new Party();
sender.setInternalId("TEST");
header.setSender(sender);
edi.setInterchangeHeader(header);

Smooks smooks = new Smooks("edi-output-smooks-config.xml");

ExecutionContext executionContext = smooks.createExecutionContext();
Exports exports = Exports.getExports(smooks.getApplicationContext());
Result[] results = exports.createResults();

smooks.filterSource(executionContext, new JavaSource(edi), results);

List<Object> objects = Exports.extractResults(results, exports);
JavaResult.ResultMap map = (JavaResult.ResultMap) objects.get(0);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();
UNEdifactInterchange41 u = (UNEdifactInterchange41) map.get("unEdifactInterchange");

// Should output EDI message as String, but StringWriter is empty
StringWriter ediOutStream1 = new StringWriter();
factory.toUNEdifact(u, ediOutStream1);

我得到了相同的结果,最后是一个空字符串.

I had the same result, an empty string in the end.

但是我认为问题是您使用了骗子.我只在Apache Camel中使用了smooks,所以我不知道这是否是进行smook转换的正确方法,但是我认为在这种情况下,您不必调用smook,您的输入是一个对象EDI,而您想要以EDI格式打印对象.

But i think the problem is your use of smooks. I only used smooks with Apache Camel, so i don't know if that is the correct way to make a transformation in smooks, but i think that in this case you dont have to call smook, your input is an object EDI and you want to print you object in EDI Format.

您唯一要做的是:

UNEdifactInterchange41 edi = new UNEdifactInterchange41();
UNB41 header = new UNB41();
header.setSender(null);
Party sender = new Party();
sender.setInternalId("TEST");
header.setSender(sender);
edi.setInterchangeHeader(header);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();

StringWriter ediOutStream1 = new StringWriter();
factory.toUNEdifact(edi, ediOutStream1);

我已经测试过了,您得到了:

I already tested, and you get this:

UNB++::TEST'

我希望这可以为您提供帮助

I hope that this could help you

这篇关于Smooks:从Java输出EDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆