目录

springboot整合cxf

建工程

新建maven项目:springboot-cxf

引pom

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.eh</groupId>
    <artifactId>springboot-cxf</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.4</version>
        </dependency>
    </dependencies>

</project>

业务类

HelloService

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.eh.cxf;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

// 可以指定使用targetNamespace指定命名空间,不指定默认包名倒置
@WebService
public interface HelloService {

    @WebMethod(operationName = "sayHello")
    @WebResult(name = "retValue")
    String sayHello(@WebParam(name = "who") String who);
}

HelloServiceImpl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package com.eh.cxf;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String who) {
        return "hello, " + who;
    }
}

配置类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.eh.cxf;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {
    private final Bus bus;
    private final HelloService helloService;

    public CxfConfig(Bus bus, HelloService helloService) {
        this.bus = bus;
        this.helloService = helloService;
    }

    /**
     * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/hello?wsdl
     * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/cxf/hello?wsdl
     */
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
    }

    /**
     * 指定url,发布服务
     *
     * @return
     */
    @Bean
    public Endpoint cxfEndpoint() {
        Endpoint endpoint = new EndpointImpl(bus, helloService);
        endpoint.publish("/hello");
        return endpoint;
    }
}

启动类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package com.eh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootCxfApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootCxfApplication.class, args);
    }
}

验证

运行程序,访问http://127.0.0.1:8080/cxf/hello?wsdl

http://img.cana.space/picStore/20201109221954.png

Java客户端访问

使用wsdl2java工具生成客户端代码

20201109222411

运行结果:

1
2
3
objc[92763]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java (0x102af14c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x102b674e0). One of the two will be used. Which one is undefined.
Invoking sayHello...
sayHello.result=hello, 徐达