springboot集成CXF

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


            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.3.0
        

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

wsdl2java -p com -d src -all  test.wsdl 
-p  指定其wsdl的命名空间,也就是要生成代码的包名: 
-d  指定要产生代码所在目录 
-client 生成客户端测试web service的代码 
-server 生成服务器启动web  service的代码 
-impl 生成web service的实现代码 
-ant  生成build.xml文件 
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.  

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();
        }
    }
}

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;
    }
}

你可能感兴趣的:(springboot集成CXF)