基于apache-CXF框架发布webservice

1、maven依赖:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.4</version>
</dependency>

2、spring集成cxf webservice

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">
        
		<bean class="org.apache.cxf.jaxws.context.WebServiceContextImpl">
        </bean>
        <!--其中id是自己定义的,implementor是接口实现类,address就是访问的地址 -->
        <!-- 相当于Endpoint.publish("http://localhost:8080/service", newHelloServiceImp()); -->
        <bean class="org.apache.cxf.jaxws.context.WebServiceContextImpl">
        </bean>

        <jaxws:endpoint implementor="com.wisdombud.product.sharedplatform.entity.service.impl.EntityListWebServiceImpl"
                        address="/queryList">
        </jaxws:endpoint>

        <jaxws:endpoint implementor="com.wisdombud.product.sharedplatform.entity.service.impl.EntityDetailWebserviceImpl"
                        address="/queryDetail">
        </jaxws:endpoint>
</beans>

3、接口

import javax.jws.WebService;

/**
 * 功能 :
 * date : 2019-06-11 10:47
 *
 * @version : 0.0.4-snapshot
 * @Author : txzhang@wisdombud.com
 * @since : JDK 1.8
 */
@WebService
public interface EntityListWebService {

    String getListData(Long appId, String key, Integer start, Integer limit);

}
@WebService
@Service
public class EntityListWebServiceImpl implements EntityListWebService {

	@Autowired
    private WebServiceContext webServiceContext;
    
    @Override
    public String getListData(final Long appId, final String key, final Integer start, final Integer limit) {
      	//获取request对象
    	final HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
    	
    	...业务代码
    	
      	return "张三";
    }
}

4、webservice拦截器实现

import java.util.HashMap;

/**
 * 功能 : webservice 拦截器
 * date : 2019-06-11 11:52
 *
 * @version : 0.0.4-snapshot
 * @Author : txzhang@wisdombud.com
 * @since : JDK 1.8
 */
public class WebserviceInterceptor extends AbstractPhaseInterceptor<Message> {


    public WebserviceInterceptor() {
        //需要把握好拦截的时机
        super(Phase.PRE_INVOKE);
    }

    @Override
    public void handleMessage(final Message message) throws Fault {
    	//用来加载调用者传递的参数
        final HashMap map = message.getContent(HashMap.class);
        System.out.println(map.toString());
    }
}

5、客户端测试,我这里通过cxf,写的main方法测试的

/**
 * 功能 :
 * date : 2019-06-11 14:52
 *
 * @version : 0.0.4-snapshot
 * @Author : txzhang@wisdombud.com
 * @since : JDK 1.8
 */
public class CxfClientTest {

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean ();
        factoryBean.setAddress("http://localhost/ws/queryList?wsdl");
        factoryBean.setServiceClass(EntityListWebService.class);
        final EntityListWebService service = (EntityListWebService) factoryBean.create();
        System.out.println(service.getListData(11648L, "bed1bd3b-4bde-4d0c-9dca-94fab3893260", 0, 10));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值