WebService服务--SpringBoot整合Cxf

本文介绍了如何使用SpringBoot结合Cxf创建并发布WebService服务。从创建SpringBoot项目开始,逐步讲解了添加依赖、定义服务接口和实现类,以及配置服务发布的详细步骤,最后展示了项目启动后生成的wsdl信息。

一.创建SpringBoot项目

二.导入必需jar(pom.xml)

<!--webService依赖-->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>

三. 服务接口

package com.joinforwin.hiswebservice;

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

/**
 * @author gj
 * @date 2020/3/17
 */
//指定命名空间,地址是所在包名的反写
@WebService(targetNamespace = "http://hiswebservice.joinforwin.com")
public interface UserService {

    @WebMethod(operationName = "getHello")//在wsdl文档中显示的方法名,@WebMethod可不指定,默认与方法名相同
    String getHello(@WebParam(name="name") String name );}

四.接口实现类

package com.joinforwin.hiswebservice;

import org.springframework.stereotype.Component;

/**
 * @author : gj
 * create at:  2020/3/17  9:59 AM
 * @description:
 */
@Component
public class UserServiceImpl implements UserService {

    @Override
    public String getHello(String name) {
        return "hello " + name;
    }
}

五.服务发布配置类

package com.joinforwin.hiswebservice.config;

import com.joinforwin.hiswebservice.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.*;


@Configuration
public class CxfConfig {

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    
     /**
     * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
     * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/test/user?wsdl
     */
    @Bean
    public ServletRegistrationBean webServiceServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/test/*");
    }

    @Autowired
    private UserService userService;

    /**
     * 发布服务
     * 指定访问url
     * @return
     */
    @Bean
    public Endpoint userEndpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(),userService);
        endpoint.publish("/user");
        return endpoint;
    }

}
    

六.项目启动后的wsdl信息

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值