springboot集成CXF提供webservier服务和客户端

1、pom.xml引入cxf,不同版本的cxf与springboot的版本要匹配,具体上maven repository查看。

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.0</version>
</dependency>

2、根据webservier接口生成客户端代码(可忽略)

2、去官网下载apache-cxf
http://cxf.apache.org/download.html
3、把解压后apache-cxf的bin目录配置到环境变量,以便使用wsdl2java命令
4、生成client端代码:wsdl2java -all -encoding utf-8 http://localhost:8080/services/CommonService?wsdl

5、客户端代码(生成的代码只需要取service和entity即可):

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


public class Test {
    public static void main(String[] args) {
        try {
            // 接口地址
            String address = "http://localhost:8080/services/CommonService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
            // 创建一个代理接口实现
            CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            User user = new User();
            user.setName("ee");
            user.setAge(5);
            String result = cs.saveUser(user);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  
@Bean
public CommonService commonService(){
    // 接口地址
String address = "http://127.0.0.1/common/webservice/lbsca/LBPDSUpgrade";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(
CommonService.class);
    // 创建一个代理接口实现
CommonServicecs = (CommonServicecs) jaxWsProxyFactoryBean.create();
    return cs;
}
}

6、server端发布

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

/**
 * 接口
 * 
 * @author leftso
 *
 */
@WebService(name = "CommonService", // 暴露服务名称
        targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    public String sayHello(@WebParam(name = "userName") String name);
    
    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    public String saveUser(@WebParam(name = "user") User user);

}
import javax.jws.WebService;

import org.springframework.stereotype.Component;

/**
 * 接口实现
 * 
 * @author leftso
 *
 */
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
        targetNamespace = "http://webservice.leftso.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.example.demo.ws.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService {

    @Override
    public String sayHello(String name) {

        return "Hello ," + name;
    }

    @Override
    public String saveUser(User user) {
        // TODO Auto-generated method stub
        return "ok!" + user.getName();
    }

}
import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;
 
    @Autowired
    CommonService commonService;
 
    /** JAX-WS **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, commonService);
        endpoint.publish("/CommonService");
        return endpoint;
    }
}

 

posted @ 2022-09-30 16:37  扰扰  阅读(405)  评论(0编辑  收藏  举报