springboot2.0 - WebService 集成记录,使用cxf调用接口

原创
06/28 13:16
阅读数 101

 

 

首先webservice 区分调用与提供

调用方式存在两种

1. 动态调用:  基于 使用 JaxWsDynamicClientFactory 调用接口


import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
@Slf4j
public class WebserviceClientUtil {
    public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(operationName, params);
        log.info("webservice返回数据: " + objects[0].toString());
        return objects[0].toString();
    }
}

import org.junit.jupiter.api.Test;

public class WsdlTest {
    @Test
    public void t1() throws Exception {
        String wsdUrl = "http://168.168.0.205/cidp/ws/xxServiceWSXML?wsdl";
        String methodName = "xxxModelDatasXMLWithVague";
        // 参数非必须填时, 但还是需要传入, 不能不传入
        String[] parms = new String[]{"RY","","",""};
        String rtObj = WebserviceClientUtil.callWebSV(wsdUrl,methodName,parms);
        System.out.println(rtObj);
    }
}

2.静态调用: 自己模拟生成接口提供方的接口及业务代码, 然后像调用本地方法一样直接调用 

模拟生成接口: 使用cxf 插件, 根据wsdl 路径生成本地代码

<plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <!--<sourceRoot>src/main/java</sourceRoot>-->
                            <defaultOptions>
                                <extraargs>
                                    <extraarg>-impl</extraarg>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-validate</extraarg>
                                    <extraarg>-client</extraarg>
                                </extraargs>
                            </defaultOptions>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>http://168.168.0.205/cidp/ws/xx/ServiceWSXML?wsdl</wsdl>
                                    <!--<wsdl>D:\code\Demo\src/main/resources/HVSService.wsdl</wsdl>-->
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>com.xx.xx.admin.wsdl</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

引入后; install 下maven 项目 , 生成代码路径在target/generated 下 

(即extaarg 设定的路径下) ; 复制生成的代码至项目代码中接口, 插件使用后, 即可注释

找到接口类, 不需要修改其他, 引用类, 像调用本地方法一样调用执行即可;


import com.xx.xxx.admin.wsdl.XXXXServiceWSXML;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.jupiter.api.Test;

public class WsdlTest {
    @Test
    public void t1(){
        System.out.println("请求开始----");
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setAddress("http://168.168.0.205/cidp/ws/xxxfsServiceWSXML");
        jaxWsProxyFactoryBean.setServiceClass(XXXsServiceWSXML.class);
        XxxxsServiceWSXML demo = (XxxxsServiceWSXML) jaxWsProxyFactoryBean.create();
        String rtStr = demo.getDetail("RY","","","");
        System.out.println(rtStr);

        System.out.println("请求结束");
    }


    @Test
    public void t2(){
        System.out.println("--------");
    }
}

 

 

webservice 所需依赖

<!-- webService -->
        <!-- webservice cxf -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
        <!--        // 第一个方式没有第三个依赖会报错 Cannot find any registered HttpDestinationFactory from the Bus.-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.6</version>
        </dependency>
    </dependencies>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部