SpringBoot集成WebService

介绍

  • SpringBoot集成WebService

示例

  • 1.创建服务端项目添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<!-- CXF webservice -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.4.3</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.4.3</version>
</dependency>
  • 2.添加配置类
@Configuration
public class WebServiceConfig {

    /**
     * Bus交由spring管理
     * @return
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 服务地址:
     * http://localhost:8080/services
     * http://localhost:8080/services/v1/demo?wsdl
     * @param helloService
     * @return
     */
    @Bean
    public Endpoint endpoint(HelloService helloService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloService);
        endpoint.publish("/v1/demo");
        return endpoint;
    }
}
  • 3.创建服务接口
@WebService(name = "HelloService", targetNamespace = "http://service.server.webservice.example.com")
public interface HelloService {

    @WebMethod
    String hello(@WebParam String msg);
}

@Service
@WebService(name = "HelloService", targetNamespace = "http://service.server.webservice.example.com",
        endpointInterface = "com.example.webservice.server.service.HelloService")
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello(String msg) {
        return "Hello, " + msg;
    }
}
  • 4.创建客户端项目并添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<!-- CXF webservice -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.4.3</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.4.3</version>
</dependency>
  • 5.客户端发起请求调用
public class MyClient {

    public static void main(String[] args) throws Exception {
        //方式1,通过代理工厂,根据接口创建代理实现请求
        test1();

        //方式2,通过动态客户端方式请求
        test2();

        //方式3,通过"Generate Java Code From Wsdl"生成代码(idea中选中文件夹右键 -> webservices -> Generate Java Code From Wsdl)
        test3();

    }

    private static void test1() {
        //创建代理工厂
        JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
        //设置代理地址
        proxy.setAddress("http://localhost:8080/services/v1/demo?wsdl");
        //设置被代理类接口
        proxy.setServiceClass(HelloService.class);
        //接口代理
        HelloService service = (HelloService) proxy.create();
        //调用
        String response = service.hello("World");
        System.out.println("test1返回结果:" + response);
    }

    private static void test2() throws Exception {
        //创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/v1/demo?wsdl");
        //调远程方法
        Object[] response = client.invoke("hello", "World");
        System.out.println("test2返回结果:" + response[0]);
    }

    private static void test3() {
        HelloServiceImplService service = new HelloServiceImplService();
        String response = service.getHelloServicePort().hello("World");
        System.out.println("test3返回结果:" + response);
    }
}
  • 码云 https://gitee.com/hweiyu/spring-boot-webservice.git
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值