如何配置OpenAPI实现SOAP转REST,避免请求URL附加REST路径?
如何让OpenAPI请求直接指向SOAP服务URL,不附加REST路径?
我正在编写OpenAPI,将SOAP API以REST形式在Swagger上展示。使用的公开SOAP服务地址是https://www.dataaccess.com/webservicesserver/NumberConversion.wso,通过Java程序转换为OpenAPI格式。现在想让请求直接使用该SOAP服务URL,而不在请求时附加/NumberToWords、/NumberToDollars这些REST路径,咨询是否可行及配置方法。
当前OpenAPI配置如下:
openapi: 3.0.1 info: title: SOAP API to REST API version: 3.0.3 servers: - url: https://www.dataaccess.com/webservicesserver/NumberConversion.wso paths: /NumberToWords: post: operationId: NumberToWords requestBody: content: text/xml: schema: $ref: '#/components/schemas/rootElement_NumberToWords' required: true responses: default: description: Default Description content: text/xml charset=UTF-8: schema: $ref: '#/components/schemas/rootElement_NumberToWordsResponse' x-wso2-soap: x-soap-style: document soap-action: "" x-soap-message-type: document soap-operation: NumberToWords namespace: http://www.dataaccess.com/webservicesserver/ x-soap-version: "1.2" /NumberToDollars: post: operationId: NumberToDollars requestBody: content: text/xml: schema: $ref: '#/components/schemas/rootElement_NumberToDollars' required: true responses: default: description: Default Description content: text/xml charset=UTF-8: schema: $ref: '#/components/schemas/rootElement_NumberToDollarsResponse' x-wso2-soap: x-soap-style: document soap-action: "" x-soap-message-type: document soap-operation: NumberToDollars namespace: http://www.dataaccess.com/webservicesserver/ x-soap-version: "1.2" components: schemas: NumberToWordsResult: type: string xml: {} rootElement_NumberToWordsResponse: type: object properties: Body: type: object properties: NumberToWordsResponse: type: object properties: NumberToWordsResult: type: string xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap dNum: type: integer xml: {} rootElement_NumberToDollars: type: object properties: Body: type: object properties: NumberToDollars: type: object properties: dNum: type: integer xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap ubiNum: type: integer xml: {} rootElement_NumberToWords: type: object properties: Body: type: object properties: NumberToWords: type: object properties: ubiNum: type: integer xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap NumberToDollarsResult: type: string xml: {} rootElement_NumberToDollarsResponse: type: object properties: Body: type: object properties: NumberToDollarsResponse: type: object properties: NumberToDollarsResult: type: string xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap
解决方案
完全可以实现,只需调整paths下的路径配置,将所有操作指向根路径/,这样请求就会直接发送到servers中配置的SOAP服务完整URL,不会附加额外路径。修改后的配置如下:
openapi: 3.0.1 info: title: SOAP API to REST API version: 3.0.3 servers: - url: https://www.dataaccess.com/webservicesserver/NumberConversion.wso paths: /: post: operationId: NumberToWords requestBody: content: text/xml: schema: $ref: '#/components/schemas/rootElement_NumberToWords' required: true responses: default: description: Default Description content: text/xml charset=UTF-8: schema: $ref: '#/components/schemas/rootElement_NumberToWordsResponse' x-wso2-soap: x-soap-style: document soap-action: "" x-soap-message-type: document soap-operation: NumberToWords namespace: http://www.dataaccess.com/webservicesserver/ x-soap-version: "1.2" post: operationId: NumberToDollars requestBody: content: text/xml: schema: $ref: '#/components/schemas/rootElement_NumberToDollars' required: true responses: default: description: Default Description content: text/xml charset=UTF-8: schema: $ref: '#/components/schemas/rootElement_NumberToDollarsResponse' x-wso2-soap: x-soap-style: document soap-action: "" x-soap-message-type: document soap-operation: NumberToDollars namespace: http://www.dataaccess.com/webservicesserver/ x-soap-version: "1.2" components: schemas: NumberToWordsResult: type: string xml: {} rootElement_NumberToWordsResponse: type: object properties: Body: type: object properties: NumberToWordsResponse: type: object properties: NumberToWordsResult: type: string xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap dNum: type: integer xml: {} rootElement_NumberToDollars: type: object properties: Body: type: object properties: NumberToDollars: type: object properties: dNum: type: integer xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap ubiNum: type: integer xml: {} rootElement_NumberToWords: type: object properties: Body: type: object properties: NumberToWords: type: object properties: ubiNum: type: integer xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap NumberToDollarsResult: type: string xml: {} rootElement_NumberToDollarsResponse: type: object properties: Body: type: object properties: NumberToDollarsResponse: type: object properties: NumberToDollarsResult: type: string xml: {} xml: namespace: http://www.dataaccess.com/webservicesserver/ xml: prefix: soap xml: name: Envelope namespace: http://schemas.xmlsoap.org/soap/envelope/ prefix: soap
关键修改说明
- 将
paths下的/NumberToWords和/NumberToDollars合并为根路径/,这样请求URL就是servers中配置的完整SOAP服务地址,不会附加任何额外路径。 - 保留两个
post操作的operationId唯一,Swagger UI会基于该ID区分不同的操作选项,方便用户选择调用对应的SOAP接口。 - 原有的
x-wso2-soap配置保持不变,确保每个操作正确映射到对应的SOAP操作、命名空间和SOAP版本。
内容的提问来源于stack exchange,提问作者xxestter
相关产品推荐
相关产品推荐

