gpt4 book ai didi

java - 将 iDOC 从 Java 发送到 SAP

转载 作者:行者123 更新时间:2023-12-04 17:48:15 26 4
gpt4 key购买 nike

我们有从我们的系统生成的 iDOC 文件。现在我们正尝试通过 RFC 连接将它们发送到 SAP。我已经作为客户端建立了一个 RFC 连接,但我无法发送 iDOC!

我试图创建一个样本 iDOC 来测试它,但它不起作用!

Exception in thread "main" java.lang.NoSuchMethodError: com.sap.conn.idoc.jco.SAPRepository$ConnectionProvider.execute(Lcom/sap/conn/jco/JCoFunction;Lcom/sap/conn/jco/rt/AbapRepository;)V
at com.sap.conn.idoc.jco.SAPRepository.queryRootSegmentMetaData40(SAPRepository.java:1204)
at com.sap.conn.idoc.jco.SAPRepository.queryRootSegmentMetaData(SAPRepository.java:1110)
at com.sap.conn.idoc.jco.SAPRepository.getRootSegmentMetaData(SAPRepository.java:909)
at com.sap.conn.idoc.rt.DefaultIDocDocument.<init>(DefaultIDocDocument.java:124)
at com.sap.conn.idoc.rt.DefaultIDocDocument.<init>(DefaultIDocDocument.java:61)
at com.sap.conn.idoc.jco.JCoIDocDocument.<init>(JCoIDocDocument.java:51)
at com.sap.conn.idoc.jco.JCoIDocRuntime.createIDocDocument(JCoIDocRuntime.java:133)
at com.sap.conn.idoc.jco.JCoIDocRuntime.createIDocDocument(JCoIDocRuntime.java:35)
at com.sap.conn.idoc.rt.DefaultIDocRuntime.createIDocDocument(DefaultIDocRuntime.java:175)
at com.sap.conn.idoc.rt.DefaultIDocRuntime.createIDocDocument(DefaultIDocRuntime.java:18)
at com.idoc.sender.IDocClientExample.main(IDocClientExample.java:49)

我什至尝试发送示例 iDOC XML,但我也遇到了另一个错误,多次尝试更改 XML,但我一次又一次地面临同样的问题!

com.sap.conn.idoc.IDocParseException: (7) IDOC_ERROR_PARSE_FAILURE: Invalid character encountered in XML input data source:
state=READING_XMLDECLARATION, charPosition=14, lineNumber=1, columnNumber=15, invalidChar=U+003D,
sourceSnippet=...@<?xml version="1.0" encoding="UTF-8"?><DEBMAS06><IDOC><EDI_DC40><DOCNUM>20120114120000</DOCNUM><IDO...
^
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.throwIDocParseException(DefaultIDocXMLParser.java:2223)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.parseProlog(DefaultIDocXMLParser.java:1635)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.parse(DefaultIDocXMLParser.java:320)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:112)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:54)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:31)
at com.idoc.sender.IDocClientExample.main(IDocClientExample.java:57)

这是我用来创建 iDOC 和发送 iDOC XML 的代码:

package com.idoc.sender;


import com.sap.conn.idoc.*;
import com.sap.conn.idoc.jco.JCoIDoc;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;

import java.io.BufferedReader;
import java.io.FileReader;


public class IDocClientExample {

public static void main(String[] args) {

try {

String iDocXML = null;
FileReader fileReader;

try {
fileReader = new FileReader("TestSalesOrder.xml");
BufferedReader br = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}

iDocXML = sb.toString();

br.close();
fileReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}

// see provided configuration file xxxx.jcoDestination
JCoDestination destination = JCoDestinationManager.getDestination("xxxx");
IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
String tid = destination.createTID();
IDocFactory iDocFactory = JCoIDoc.getIDocFactory();

System.out.println(destination.getAttributes());


// a) create sample new idoc
IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "SYSTAT01");
IDocSegment segment = doc.getRootSegment();
segment.addChild("E1MARAM");
JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);


// b) use existent xml file
IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
destination.confirmTID(tid);

} catch (Exception e) {
e.printStackTrace();
}
System.out.print("program end");
}
}
// a) create sample new idoc
IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "SYSTAT01");
IDocSegment segment = doc.getRootSegment();
segment.addChild("E1MARAM");
JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);


// b) use existent xml file
IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
destination.confirmTID(tid);

} catch (Exception e) {
e.printStackTrace();
}
System.out.print("program end");
}
}

这是我的连接文件:

jco.client.lang=EN
jco.client.client=100
jco.client.passwd=xxxx
jco.client.user=xxxx
jco.client.sysnr=01
jco.client.ashost=xxxx

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<DEBMAS06>
<IDOC>
<EDI_DC40>
<DOCNUM>20120114120000</DOCNUM><IDOCTYP>DEBMAS06</IDOCTYP>
<MESTYP>DEBMAS</MESTYP>
<SNDPOR>HTTPDEMO</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>HTTPDEMO</SNDPRN>
<RCVPOR>SAPxxx</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>xxxCLNT100</RCVPRN>
</EDI_DC40>
<E1KNA1M segment="1">
<KUNNR>47</KUNNR>
<NAME1>Test Customer</NAME1>
<KTOKD>0004</KTOKD>
<SORTL>TEST</SORTL>
<ORT01>City</ORT01>
<LAND1>PL</LAND1>
<LZONE>A</LZONE>
<SPRAS>L</SPRAS>
<STRAS>Street</STRAS>
</E1KNA1M>
</IDOC>
</DEBMAS06>

我正在使用 sapjco3-x86-64bit-3.0.5 和 sapidoc-3.0.1!

测试 iDOC 的创建和发送 IDOC XML 只是为了测试,但在我们的例子中,我们已经准备好 iDOC!

我的问题是,如何通过 RFC 将 iDOC 直接从我的磁盘发送到 SAP?如果我不能这样做,我如何解决创建 iDOC 或发送 iDOC XML 的错误?我的第二个问题,一旦我能够发送 iDOC,我是否必须建立一个新的 SERVER RFC 连接以便我可以从 SAP 接收 iDOC?

** 编辑 **

现在我可以正确发送和接收 IDoc。我的问题是如何将 IDoc 作为 Flat IDoc 获取。

我可以使用以下代码将我的 IDoc 作为 XML IDoc 接收:

package com.idoc.actif;

import com.sap.conn.idoc.IDocDocumentList;
import com.sap.conn.idoc.IDocXMLProcessor;
import com.sap.conn.idoc.jco.*;
import com.sap.conn.jco.server.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class IDocServerExample
{
public static void main(String[] a)
{
try
{
// see provided examples of configuration files xxxx.jcoServer and xxxx.jcoDestination
JCoIDocServer server = JCoIDoc.getServer("xxxx");
server.setIDocHandlerFactory(new MyIDocHandlerFactory());

server.setTIDHandler(new MyTidHandler());

MyThrowableListener listener = new MyThrowableListener();
server.addServerErrorListener(listener);
server.addServerExceptionListener(listener);
//server.setConnectionCount(1);
server.start();

}
catch (Exception e)
{
e.printStackTrace();
}
}

static class MyIDocHandler implements JCoIDocHandler
{
public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
{

FileOutputStream fos=null;
OutputStreamWriter osw=null;
try
{
//receiving XML IDoc, how to get the IDoc as a Flat IDoc (SAP is sending the IDoc as Flat)???
IDocXMLProcessor xmlProcessor = JCoIDoc.getIDocFactory().getIDocXMLProcessor();
fos=new FileOutputStream(serverCtx.getTID());
osw=new OutputStreamWriter(fos, "UTF8");
xmlProcessor.render(idocList, osw, IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);
osw.flush();
}
catch (Throwable thr)
{
thr.printStackTrace();
}
finally
{
try
{
if (osw!=null)
osw.close();
if (fos!=null)
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
{
private JCoIDocHandler handler = new MyIDocHandler();
public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
{
return handler;
}
}

static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
{

public void serverErrorOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Error error)
{
System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
error.printStackTrace();
}
public void serverExceptionOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Exception error)
{
System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
error.printStackTrace();
}
}

static class MyTidHandler implements JCoServerTIDHandler
{
public boolean checkTID(JCoServerContext serverCtx, String tid)
{
System.out.println("checkTID called for TID="+tid);
return true;
}


public void confirmTID(JCoServerContext serverCtx, String tid)
{
System.out.println("confirmTID called for TID="+tid);
}


public void commit(JCoServerContext serverCtx, String tid)
{
System.out.println("commit called for TID="+tid);
}


public void rollback(JCoServerContext serverCtx, String tid)
{
System.out.print("rollback called for TID="+tid);
}
}
}

最佳答案

我猜您使用的两个 SAP 库之间存在不兼容性。我建议首先安装最新的补丁级别。目前这是 JCo 版本 3.0.17 和 Java IDoc 库 3.0.12。

我只是不明白你在这里用 StringBuffer 做什么。如果您想从文件中读取 IDoc-XML,那么 IDocXMLProcessor 会为此提供适当的解析方法 - 最好的方法是

IDocXMLProcessor.parse(IDocRepository repository, InputStream xmlStream)

这样它也可以处理包含的 XML 编码属性中指定的不同文件编码。如果您选择 java.io.Reader 或 String,那么您必须自己处理(您在示例中没有这样做)。

但原则上你的代码乍一看似乎没有错。与 IDoc-XML 本身相比,后者缺少 标记的强制 BEGIN="1"和 SEGMENT="1"属性。

关于java - 将 iDOC 从 Java 发送到 SAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47289682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com