springboot整合cxf

 代码结构

配置类 

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;

import com.asiainfo.group.webservice.HelloService;

@Configuration
public class CxfConfig {
	@Autowired
    private Bus bus;

    @Autowired
    HelloService helloService;

    //默认是service
    /*@Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }*/

    
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloService);
        endpoint.publish("/hello");
        return endpoint;
    }

}

webservice接口 

 

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

@WebService
public interface HelloService {
	//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息
	@WebMethod
    public String getUsername(@WebParam(name = "userId") String userId);

    @WebMethod
    @WebResult(name="String",targetNamespace="")
    public String getUserAge(@WebParam(name = "userId") String userId);

}

webservice接口实现

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.asiainfo.group.webservice.HelloService;



@WebService(serviceName="helloService",//对外发布的服务名
targetNamespace="http://webservice.group.asiainfo.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.asiainfo.group.webservice.HelloService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Component
public class HelloServiceImpl implements HelloService{

	@Override
	public String getUsername(String userId) {
		
		return userId+":zhangsan";
	}

	@Override
	public String getUserAge(String userId) {
		
		return userId+":18";
	}

}

cxf客户端动态调用 

 

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


public class CxfClient {

	

    public static void main(String[] args) throws Exception {
    	String wsdl="http://localhost:8080/services/hello?wsdl";
    	//最后的斜杠不能少
    	String targetNamespace = "http://webservice.group.asiainfo.com/";
        String methodName ="getUsername";
        String userId="100001";
        Object[] params=new Object[]{userId};
        
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdl);
        
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects;
            
        objects = client.invoke(new QName(targetNamespace, methodName), params);
        System.out.println("返回数据:" + objects[0]);
        
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波波老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值