You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot SOAP客户端如何指向Azure API Manager的Base URL?

解决Spring Boot SOAP客户端指向Azure API Manager地址的问题

方法一:通过Service类动态设置Endpoint

你已经生成了继承jakarta.xml.ws.Service的WebServiceFactory,可以直接在创建服务实例时指定Azure APIM的目标地址:

// 实例化WebServiceFactory
WebServiceFactory serviceFactory = new WebServiceFactory();
// 获取PutMessageSoapType服务实例
PutMessageSoapType soapClient = serviceFactory.getPutMessageSoapTypePort();

// 绑定Azure APIM的Endpoint地址
BindingProvider bindingProvider = (BindingProvider) soapClient;
bindingProvider.getRequestContext().put(
    BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
    "https://alfdevapi5-example-apim.azure-api.net/suffix1"
);

方法二:Spring Bean配置中直接指定地址

如果用Spring管理客户端实例,可在配置类里完成地址绑定,还能顺便处理APIM的订阅密钥认证:

@Configuration
public class SoapClientConfig {

    @Bean
    public PutMessageSoapType putMessageSoapType() {
        WebServiceFactory serviceFactory = new WebServiceFactory();
        PutMessageSoapType soapClient = serviceFactory.getPutMessageSoapTypePort();
        
        BindingProvider bindingProvider = (BindingProvider) soapClient;
        // 设置Endpoint地址
        bindingProvider.getRequestContext().put(
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            "https://alfdevapi5-example-apim.azure-api.net/suffix1"
        );
        
        // 若APIM要求订阅密钥,添加请求头
        Map<String, List<String>> headers = new HashMap<>();
        headers.put("Ocp-Apim-Subscription-Key", Collections.singletonList("你的APIM订阅密钥"));
        bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        
        return soapClient;
    }
}

方法三:通过配置文件灵活管理地址

把地址和密钥放到application.properties里,方便环境切换:

# application.properties
soap.client.endpoint=https://alfdevapi5-example-apim.azure-api.net/suffix1
soap.client.subscription-key=你的APIM订阅密钥

然后在配置类中读取配置:

@Configuration
@ConfigurationProperties(prefix = "soap.client")
public class SoapClientConfig {
    private String endpoint;
    private String subscriptionKey;

    // 生成getter和setter

    @Bean
    public PutMessageSoapType putMessageSoapType() {
        WebServiceFactory serviceFactory = new WebServiceFactory();
        PutMessageSoapType soapClient = serviceFactory.getPutMessageSoapTypePort();
        
        BindingProvider bindingProvider = (BindingProvider) soapClient;
        bindingProvider.getRequestContext().put(
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            endpoint
        );
        
        // 注入订阅密钥头部
        Map<String, List<String>> headers = new HashMap<>();
        headers.put("Ocp-Apim-Subscription-Key", Collections.singletonList(subscriptionKey));
        bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        
        return soapClient;
    }
}

注意:Azure API Manager默认启用订阅密钥认证,必须在请求头中携带Ocp-Apim-Subscription-Key,否则请求会被拦截。

内容的提问来源于stack exchange,提问作者Skip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 00:42:09