1、基于 PI 的 Webservice 发布实例 分类: ABAP PI 2011-08-21 21:22 1442 人阅读 评论(1) 收藏 举报 最近的项目中,接口较多,而 Webservice 技术是主要实现方式之一。下面以项目中的一个具体实例来体验一下基于 PI 的 Webservice 发布。业务场景SAP 系统实时接收一个外围接口系统(基于 Web 的电子商务平台)回传的数据。技术分析由于同步要求较高,数据量偏小,采用 Webservice 实现较为合理。实例演示创建好自建表,用于接收回传的数据:TC: SPROXY 根据集成组在 PI 配置的服务名创建 Service Interf
2、ace在实现类中,根据传入的 XML 字符串,解析后更新到自建表:data: ls_field_data like zmmjyh_cdif,lt_field_data like table of ls_field_data,ls_zmmjyh_ht_0007 like zmmjyh_ht_0007,lt_zmmjyh_ht_0007 like table of ls_zmmjyh_ht_0007,l_retcode type i,l_fieldname type string,l_float type f.field-symbols: type any,type any.“解析 XML 数据到
3、通用内表type-pools: ixml.types: begin of t_xml_line ,data(256) type x,end of t_xml_line.data: l_ixml type ref to if_ixml,l_streamfactory type ref to if_ixml_stream_factory,l_parser type ref to if_ixml_parser,l_istream type ref to if_ixml_istream,l_document type ref to if_ixml_document,l_node type ref to
4、 if_ixml_node,l_xmldata type string.data: l_elem type ref to if_ixml_element,l_root_node type ref to if_ixml_node,l_next_node type ref to if_ixml_node,l_name type string,l_iterator type ref to if_ixml_node_iterator.data: l_xml_table type table of t_xml_line,l_xml_line type t_xml_line,l_xml_table_siz
5、e type i.* Creating the main iXML factoryl_ixml = cl_ixml=create( ).* Creating a stream factoryal_streamfactory = l_ixml-create_stream_factory( ).l_istream = l_streamfactory-create_istream_string( string = input ).* Creating a documentl_document = l_ixml-create_document( ).* Create a Parserl_parser
6、= l_ixml-create_parser( stream_factory = l_streamfactoryistream = l_istreamdocument = l_document ).* Parse the streamif l_parser-parse( ) ne 0.l_retcode = 0.return .endif.* Process the documentif l_parser-is_dom_generating( ) eq X.perform process_dom tables lt_field_data using l_document .endif.*&-*
7、& Form process_dom*&-*form process_dom tables p_i_zxml structure zmmjyh_cdifusing document type ref to if_ixml_document .data: node type ref to if_ixml_node,iterator type ref to if_ixml_node_iterator,nodemap type ref to if_ixml_named_node_map,node_parent type ref to if_ixml_node,attr type ref to if_
8、ixml_node,name type string,name1 type string,prefix type string,value type string,indent type i,count type i,index type i.node ?= document.check not node is initial.if node is initial.exit.endif.* create a node iteratoriterator = node-create_iterator( ).* get current nodenode = iterator-get_next( ).
9、* loop over all nodeswhile not node is initial.indent = node-get_height( ) * 2.indent = indent + 20.case node-get_type( ).when if_ixml_node=co_node_element.* element nodename = node-get_name( ).nodemap = node-get_attributes( ).if not nodemap is initial.* attributescount = nodemap-get_length( ).do co
10、unt times.index = sy-index - 1.attr = nodemap-get_item( index ).name = attr-get_name( ).prefix = attr-get_namespace_prefix( ).value = attr-get_value( ).“记录字段名、字段值p_i_zxml-fieldname = name .p_i_zxml-fieldvalue = value .append p_i_zxml .enddo.endif.when if_ixml_node=co_node_text orif_ixml_node=co_node
11、_cdata_section.* text nodevalue = node-get_value( ).node_parent = node-get_parent( ).name1 = node_parent-get_name( ).“记录字段名、字段值p_i_zxml-fieldname = name1 .p_i_zxml-fieldvalue = value .append p_i_zxml .endcase.node = iterator-get_next( ).endwhile.endform. “process_dom“准备数据到数据库更新内表loop at lt_field_dat
12、a into ls_field_data.clear l_fieldname.assign ls_field_data-fieldvalue to .concatenate LS_ZMMJYH_HT_0007- ls_field_data-fieldname into l_fieldname.assign (l_fieldname) to .if is assigned.if ls_field_data-fieldname = CONTRAMOUNTNUMor ls_field_data-fieldname = APPLYAMOUNT.“金额字段中科学计数法的处理clear l_float.l
13、_float = .= l_float.else.= .endif.else.it_return-zresult = 0.it_return-description = 程序异常 ,字段名不匹配!.append it_return.return.endif.“到达一条数据末尾if ls_field_data-fieldname = PREPAYID.append ls_zmmjyh_ht_0007 to lt_zmmjyh_ht_0007.clear ls_zmmjyh_ht_0007.endif.endloop.“更新到自建表if lines( lt_zmmjyh_ht_0007 ) 0 .
14、modify zmmjyh_ht_0007 from table lt_zmmjyh_ht_0007.if sy-subrc = mit work and wait.it_return-zresult = 1.it_return-description = 回传成功 !.append it_return.else.rollback work.it_return-zresult = 0.it_return-description = 回传失败 ,数据库更新异常!.append it_return.endif.else.it_return-zresult = 0.it_return-descrip
15、tion = 无数据可传输!.append it_return.endif. 把结果回传给外围系统,先创建好 Transformation然后把返回值封装成 XML 串:data:l_xstr type xstring.call transformation zmmjyhmesgjdzswsource root = it_return* RESULT XML outputresult xml l_xstroptions xml_header = no . types: begin of ty_bin,bin_data(1024) type x,end of ty_bin.data:lt_bin
16、 type table of ty_bin.“文件二进制内表data:l_len type i.check input is not initial.call function SCMS_XSTRING_TO_BINARYexportingbuffer = l_xstr* APPEND_TO_TABLE = importingoutput_length = l_lentablesbinary_tab = lt_bin.call function SCMS_BINARY_TO_STRINGexportinginput_length = l_len* FIRST_LINE = 0* LAST_LI
17、NE = 0* MIMETYPE = * ENCODING =importingtext_buffer = output-zmmjyht010response-output* OUTPUT_LENGTH =tablesbinary_tab = lt_bin* EXCEPTIONS* FAILED = 1* OTHERS = 2.if sy-subrc 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.endif. 由于是基于 PI 创建的,此时的 Web
18、service 还没有注册, TC:SOAMANAGER进入 single serviceadministration,搜索刚才创建的 webservice:Zmmjyht010 ,并 apply selection点选 Configurations 标签,创建服务:记得勾选中 User ID/Password点击 show wsdl options,并把 WSDL Format 设置为 standard,然后点选 Display selectedBindings WSDL URL,在右边会得到 WSDL 的 URL在 SICF 中,找到服务 Zmmjyht010,在登录数据里,把过程数据改为 “标准”,这是为了跳过 WSDL 的安全策略。最后,然后把此 WSDL 的 URL 给到外围系统的开发人员就可以了。