webService :生成wsdl文件

添加依赖



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar
    demo
    Demo project for Spring Boot
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
    
    
        
            junit
            junit
            4.11
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-devtools
            true
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

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

    
    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    UTF-8
                
            
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    
                    true
                
            
        
    
服务端接口:
package com.example.demo.netbar.service;

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

@WebService(name = "NetbarService",  //@WebService:将该接口发布成WebService服务器
targetNamespace = "http://service.netbar.demo.example.com")   //命名空间为包名得倒叙
public interface NetbarService {
    @WebMethod
    String sayHello(@WebParam(name = "userName") String name);
}

服务端实现类:

package com.example.demo.netbar.service;

import org.springframework.stereotype.Component;

import javax.jws.WebService;

@WebService(serviceName = "NetbarService"//服务名
        ,targetNamespace = "http://service.netbar.demo.example.com"//报名倒叙,并且和接口定义保持一致
        ,endpointInterface = "com.example.demo.netbar.service.NetbarService")//包名
@Component
public class NetbarServicesImpl implements NetbarService{
    @Override
    public String sayHello(String name) {
        return "hello" + name;
    }
}

cxf配置:

package com.example.demo.netbar.service;

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;

import javax.xml.ws.Endpoint;

@Configuration
public class cxfConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private NetbarService netbarServices;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus,netbarServices);
        endpoint.publish("/NetbarService");//接口发布在 /NetbarServices 目录下
        return endpoint;
    }

}

发布之后,wsdl路径为: 

http://localhost:8080/services/NetbarService?wsdl
wsdl信息:


















 


 



 
 



















基于cxf的客户端调用webService接口:


/**
  * 方式1.代理类工厂的方式,需要拿到对方的接口
  */

@Test
    public void cl1() {
        try {
            // 接口地址
            String address = "http://localhost:8080/services/NetbarService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean =  new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(NetbarService.class);
            // 创建一个代理接口实现
            NetbarService cs = (NetbarService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userName = "Leftso";
            // 调用代理接口的方法调用并返回结果
            String result = cs.sayHello(userName);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




   /**
     * 动态调用方式
     */
    @Test
    public void cl2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/NetbarService?wsdl");

        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello", "Leftso");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

遇到的问题:

1.启动服务时报错:

Cannot determine embedded database driver class for database type NONE

原因及解决办法:

 

你可能感兴趣的:(webService :生成wsdl文件)