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中的配置的接口地址。

最后:欢迎关注公众号:后排说

闲暇之余搞得,分享自己所知道的比较良心的APP或者网站。

你可能感兴趣的:(Java生成wsdl接口实例,并动态配置地址)