• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CXF整合SpringBoot实现

武飞扬头像
kewen_123
帮助3

步骤一、pom文件

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

步骤二、服务端

import javax.jws.WebService;
import java.util.HashMap;

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

    @Override
    public Object getTeacherById(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        return map.toString();
    }
}

学新通

步骤三、服务端配置文件

import com.example.demo.service.TeacherService;
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
	TeacherService teacherService;

	/**
	 * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
	 * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/teacher?wsdl
	 * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/teacher?wsdl
	 * @return
	 */
/*	@SuppressWarnings("all")
	@Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }*/
	
	/** JAX-WS 
	 * 站点服务
	 * **/
	@Bean
	public Endpoint teacherEndpoint() {
		EndpointImpl endpoint = new EndpointImpl(bus, teacherService);
		endpoint.publish("/teacher");
		return endpoint;
	}
}
学新通

步骤四、客户端

import com.example.demo.service.TeacherService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * 	该类提供两种不同的方式来调用webservice服务
 * 	1:代理工厂方式
 * 	2:动态调用webservice
 */
public class CxfClient {
	
	
	public static void main(String[] args) {
		refWebService("http://127.0.0.1:8080/services/teacher?wsdl","getTeacherById",1L);
	}

	/**
	 * 1.代理类工厂的方式,需要拿到对方的接口地址
	 * address : http://127.0.0.1:8080/soap/teacher?wsdl
	 * paramJson : maple
	 */
	public static Object proxyWebService(String address, Object args) {
		try {
			// 代理工厂
			JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
			// 设置代理地址
			jaxWsProxyFactoryBean.setAddress(address);
			// 设置接口类型
			jaxWsProxyFactoryBean.setServiceClass(TeacherService.class);
			// 创建一个代理接口实现
			TeacherService service = (TeacherService) jaxWsProxyFactoryBean.create();
			// 调用代理接口的方法调用并返回结果
			Object o = service.getTeacherById(Long.getLong(args.toString()));
			System.out.println(o);
			return o;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 2:动态调用
	 * address : http://127.0.0.1:8080/soap/teacher?wsdl
	 * method : getUserName
	 * paramJson : maple
	 */
	public static String refWebService(String address, String method, Object... args) {
		// 创建动态客户端
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(address);
		// 需要密码的情况需要加上用户名和密码
		// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
		Object[] objects;
		try {
			// invoke("方法名",参数1,参数2,参数3....);
			objects = client.invoke(method, args);
			System.out.println("返回数据:"   objects[0]);
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
学新通

步骤五、拦截器

import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 类说明
 * 用于調用webservices接口的安全验证拦截器
 */
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{

    public ClientLoginInterceptor(String userName, String userPass)
    {
        super(Phase.PREPARE_SEND);
        this.userName = userName;
        this.userPass = userPass;
    }

    private String userName;
    private String userPass;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPass() {
        return userPass;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    @Override
    public void handleMessage(SoapMessage soap) throws Fault
    {
        List<Header> headers = soap.getHeaders();
        Document doc = DOMUtils.createDocument();
        Element auth = doc.createElementNS("http://service.demo.example.com/","SecurityHeader");
        Element userName = doc.createElement("userName");
        Element userPass = doc.createElement("userPass");

        userName.setTextContent(this.userName);
        userPass.setTextContent(this.userPass);

        auth.appendChild(userName);
        auth.appendChild(userPass);

        headers.add(0, new Header(new QName("SecurityHeader"),auth));
    }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfeiiah
系列文章
更多 icon
同类精品
更多 icon
继续加载