其他分享
首页 > 其他分享> > springboot整合cxf

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

 

标签:cxf,springboot,userId,整合,org,import,webservice,public,String
来源: https://blog.51cto.com/u_14643435/2845677