Groovy SOAP服务解组错误:如何适配指定SOAP请求格式?
问题描述
自研Groovy SOAP服务接收请求时,用SoapUI提交指定格式的SOAP请求触发soap:Client解组错误,提示意外元素<fin:input>,服务期望直接接收memberCode、fileContents等平级参数。
相关信息
SOAP请求示例
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fin="http://example.com/finance"> <soapenv:Header/> <soapenv:Body> <fin:uploadFile> <fin:input> <fin:memberCode>12345</fin:memberCode> <fin:fileContents>base64EncodedContent</fin:fileContents> <fin:fileName>test.pdf</fin:fileName> </fin:input> </fin:uploadFile> </soapenv:Body> </soapenv:Envelope>
错误响应示例
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <soap:Fault> <faultcode>soap:Client</faultcode> <faultstring>解组错误: 意外元素 (uri:"http://example.com/finance", local:"input")。期望的元素是 <{}memberCode>,<{}fileContents>,<{}fileName></faultstring> </soap:Fault> </soap:Body> </soap:Envelope>
原Groovy服务代码
@WebService(serviceName = "FileUploadService", targetNamespace = "http://example.com/finance") class FileUploadService { @WebMethod(operationName = "uploadFile") String uploadFile( @WebParam(name = "memberCode") String memberCode, @WebParam(name = "fileContents") String fileContents, @WebParam(name = "fileName") String fileName ) { // 业务逻辑处理 return "Upload success for member: ${memberCode}" } }
修改方案
问题核心是请求用<fin:input>节点包裹参数,但服务代码期望直接接收平级参数,以下是两种适配方式:
方式一:修改服务代码适配现有请求格式
创建参数封装类匹配<fin:input>节点结构,让JAX-WS能正确解组:
@XmlType(namespace = "http://example.com/finance") class UploadFileInput { @XmlElement(namespace = "http://example.com/finance") String memberCode @XmlElement(namespace = "http://example.com/finance") String fileContents @XmlElement(namespace = "http://example.com/finance") String fileName } @WebService(serviceName = "FileUploadService", targetNamespace = "http://example.com/finance") class FileUploadService { @WebMethod(operationName = "uploadFile") String uploadFile( @WebParam(name = "input") UploadFileInput input ) { // 通过input对象获取参数 return "Upload success for member: ${input.memberCode}" } }
方式二:修改SOAP请求适配现有服务代码(若允许调整请求)
移除<fin:input>节点,让参数直接作为uploadFile的子元素:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fin="http://example.com/finance"> <soapenv:Header/> <soapenv:Body> <fin:uploadFile> <fin:memberCode>12345</fin:memberCode> <fin:fileContents>base64EncodedContent</fin:fileContents> <fin:fileName>test.pdf</fin:fileName> </fin:uploadFile> </soapenv:Body> </soapenv:Envelope>
若必须保留现有请求格式,优先选择方式一修改服务代码。
内容的提问来源于stack exchange,提问作者Kraken
相关产品推荐
相关产品推荐

