如何在spring项目中引入wsdl并注册到springbean

前面三篇文章介绍下来我们对SSL,springboot引入HTTPS,CXF springboot的server端的代码有一定的认识了。下面一个项目我们讲解,如何把wsdl生成java,然后集成https生成 WebService 的client段的代码。为了方便其他项目引用这些Client的代码。我们单独创建一个module,方便其他项目引入依赖。代码在spring-soap-https-soap-intergration

项目目录结构


├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── moheqionglin
│   │   │           ├── CountryEndpoint
│   │   │           │   ├── CountryEndpointConfig.java
│   │   │           │   └── EnableCountryEndpointApi.java
│   │   │           └── SSLConfig.java
│   │   └── resources
│   │       ├── bindings
│   │       │   └── country-endpoint.xml
│   │       ├── keystore.p12
│   │       └── wsdls
│   │           ├── CountryEndpoint.wsdl

其中:

  • keystore.p12是SSL证书
  • CountryEndpoint.wsdl是wsdl文件
  • country-endpoint.xml 是bind文件。可选

pom

其中bindingFiles 可选可以不要这个bingdingfiles

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-soap-https</artifactId>
        <groupId>com.springboot</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springboot</groupId>
    <artifactId>spring-soap-https-soap-intergration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!--com.moheqionglin.CountryEndpoint.CountryEndpointConfig.SSLConfig Client 需要-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!-- com.moheqionglin.CountryEndpoint.CountryEndpointConfig.SSLConfig ClientProxy 需要-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!-- com.moheqionglin.CountryEndpoint.CountryEndpointConfig.SSLConfig HTTPConduit -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated-sources/java</sourceRoot>
                            <fork>true</fork>
                            <additionalJvmArgs>
                                -Djavax.net.ssl.trustStore=${basedir}/src/main/resources/keystore.p12 -Djavax.net.ssl.trustStorePassword=123456
                            </additionalJvmArgs>
                            <defaultOptions>
                                <allowElementRefs>true</allowElementRefs>
                                <noAddressBinding>true</noAddressBinding>
                            </defaultOptions>
                            <wsdlOptions>
                                <wsdlOption>
                                    <!--http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html-->
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/resources/bindings/country-endpoint.xml</bindingFile>
                                    </bindingFiles>
                                    <wsdl>${basedir}/src/main/resources/wsdls/CountryEndpoint.wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>com.moheqionglin.cxfEndpoint</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Config HTTPS

public class SSLConfig {

    private String password = "123456";
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public void configureSSLOnTheClient(Object c) {

        Client client = ClientProxy.getClient(c);
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();

        ByteArrayInputStream in = new ByteArrayInputStream(initEmailHeaderImage());
        try {
            TLSClientParameters tlsParams = new TLSClientParameters();
            tlsParams.setDisableCNCheck(true);

            //KeyStore用于存放证书,创建对象时 指定交换数字证书的加密标准
            KeyStore keyStore = KeyStore.getInstance("JKS");
            keyStore.load(in, password.toCharArray());

            //TrustManager决定是否信任对方的证书
            TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustFactory.init(keyStore);
            TrustManager[] tm = trustFactory.getTrustManagers();
            tlsParams.setTrustManagers(tm);
            httpConduit.setTlsClientParameters(tlsParams);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    /**
     * 这里因为是以 jar报的形式引入的。 因此读取文件的路径有点特殊
     * */
    private byte[] initEmailHeaderImage() {
        try {
            final String imageResource = "/keystore.p12";
            final URL url = getClass().getResource(imageResource);
            InputStream is = null;
            if(null == url){
                log.info("can not get the ssl key by the url : {}", imageResource);
                throw new RuntimeException("can not get the ssl key by the url " + imageResource);
            }

            if (url.toString().startsWith("jar:")) {
                is = getClass().getResourceAsStream(imageResource);
            } else {
                try {
                    is = new FileInputStream(new File(url.getFile()));
                } catch (FileNotFoundException e) {
                    log.error("can not find image {}", e.getMessage());
                }
            }
            if(null != is){
                try {
                    int length = is.available();
                    byte buf[] = new byte[length];
                    is.read(buf);
                    log.info("********>>Get ssl key : {}<<********", buf);
                    return buf;
                } catch (IOException e) {
                    log.error("convert email header image file to byte error, {}", e.getMessage());
                    throw new RuntimeException("convert email header image file to byte error");
                }
            }
        } catch (Exception ex) {
            log.error("get image fail! {}", ex.getMessage());
            throw new RuntimeException("get image fail!");
        }
        return null;
    }
}

spring bean


@Configuration
public class CountryEndpointConfig {
    @Value("${soap.https.endpoint}")
    private String address;

    @Autowired
    private SSLConfig sslConfig;

    @Bean("httpsCountryEndpoint")
    public CountryEndpoint getCountryEndpoint() {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(CountryEndpoint.class);
        jaxWsProxyFactoryBean.setAddress(address + "CountryEndpoint");
        CountryEndpoint manager = (CountryEndpoint) jaxWsProxyFactoryBean.create();
        ClientProxy.getClient(manager);
        sslConfig.configureSSLOnTheClient(manager);
        return manager;
    }


}

写 Enable 注解

@Target(value = {ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration({CountryEndpointConfig.class})
public @interface EnableCountryEndpointApi {

}

到这里其他项目就可以引入这个pom依赖了。 凡是想用这个CXF的Client的代码的。只要引入pom,然后引入@Enable就可以了使用了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
慷慨打赏