Java生成wsdl接口实例,并动态配置地址

来源:https://blog.csdn.net/m798469468/article/details/104008077

我这里调用的是泛微8(ecology 8)的接口(其它接口也是一样),本来想用动态调用,但是一直出问题无解,无奈就生成本地类来使用。
但是正式测试的接口地址是个问题,找了好久找到了上头那个老哥的方法,于是借鉴后放到了自己的项目里。

看代码即可

//@WebServiceClient(name = "WorkflowService", targetNamespace = "webservices.services.weaver.com.cn", wsdlLocation = "http://10.10.100.244//services/WorkflowService?wsdl")
@WebServiceClient(name = "WorkflowService", targetNamespace = "webservices.services.weaver.com.cn", wsdlLocation = "${WSDLURL}")
public class WorkflowService
        extends Service
{

    //静态变量在静态代码块加载后加载,且注解也在之后加载,完成动态注入修改注解里的参数
    public static String WSDLURL;
    private final static URL WORKFLOWSERVICE_WSDL_LOCATION;
    private final static WebServiceException WORKFLOWSERVICE_EXCEPTION;
    private final static QName WORKFLOWSERVICE_QNAME = new QName("webservices.services.weaver.com.cn", "WorkflowService");

    private static com.etc.eip.common.SpringContextUtil SpringContextUtil;
    static {
        URL url = null;
        WebServiceException e = null;
            Properties propertiesDad = new Properties();
            Properties propertiesSon = new Properties();
            try {
                propertiesDad = org.springframework.core.io.support.PropertiesLoaderUtils.loadAllProperties("application.yml");
                //遍历取值
                Set<Object> objectsDad = propertiesDad.keySet();
                String environment="";
                for (Object object : objectsDad){

                    //取到参数赋值给静态变量
                    if (object.toString().equals("active")) {
                        environment = new String(propertiesDad.getProperty((String) object).getBytes("iso-8859-1"), "gbk");
                        System.out.println("============================================================================");
                        System.out.println("environment :"+environment);
                        System.out.println("============================================================================");
                    }
                }
                if (environment.equals("test")){
                    propertiesSon = org.springframework.core.io.support.PropertiesLoaderUtils.loadAllProperties("application-test.yml");
                }else {//prod
                    propertiesSon = org.springframework.core.io.support.PropertiesLoaderUtils.loadAllProperties("application-prod.yml");
                }
                //遍历取值
                Set<Object> objects = propertiesSon.keySet();
                for (Object object : objects) {
                    //取到参数赋值给静态变量
                    if (object.toString().equals("workflowServiceWsdl")) {
                        WSDLURL = new String(propertiesSon.getProperty((String) object).getBytes("iso-8859-1"), "gbk");
                        System.out.println("============================================================================");
                        System.out.println("WSDLURL :"+WSDLURL);
                        System.out.println("============================================================================");
                    }
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                url = new URL(WSDLURL);
            } catch (MalformedURLException e2) {
                java.util.logging.Logger.getLogger(WorkflowService.class.getName())
                        .log(java.util.logging.Level.INFO,
                                "Can not initialize the default wsdl from {0}", WSDLURL);
            }
        WORKFLOWSERVICE_WSDL_LOCATION = url;
        WORKFLOWSERVICE_EXCEPTION = e;
    }

原理就是先找到application.yml中是什么环境(test 或者 prod,我们这里只有这两个环境),再去读取相应的application-test.yml或者application-prod.yml中的配置的接口地址。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值