cxf 入门实例

本文提供了一个使用Apache CXF创建和测试Web服务的详细步骤。首先从Apache官网下载CXF,然后在web.xml中配置CXFServlet。接着创建一个简单的HelloWorld接口和其实现类。在cxf-servlet.xml中配置服务地址和拦截器。部署后,通过指定URL查看生成的WSDL。最后,编写客户端测试代码进行调用。
摘要由CSDN通过智能技术生成

 

  1 首先下载cxf  www.apache.org 找到cxf 

 

   修改配置文件web.xml 添加下面一段

 

    <servlet>
  <servlet-name>CXF</servlet-name>
  <servlet-class>
   org.apache.cxf.transport.servlet.CXFServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>CXF</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>

 

2  写个简单的hellword

package test.com.launch.x431.business;

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

//标注是webservice
@WebService
public interface IHelloWorld
{
 //WebResult  是返回时wsdl 的显示名称   WebParam里面的name 是参数名称
 public @WebResult (name="result")String sayHello(@WebParam(name="text")String text);
 
}

 

3 实现

 

package test.com.launch.x431.business;

import javax.annotation.Resource;
import javax.servlet.http.*;
import javax.xml.rpc.server.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.MessageContext.Scope;
import javax.xml.ws.WebServiceContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

 


//@WebService(endpointInterface = "test.com.launch.x431.business.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
 @Resource
  private WebServiceContext context;
 public String sayHello(String text) {
  System.out.println("sayHello is called!");
   String CustomerIP="";
  //此处可以取得客户端的ip
   MessageContext ctx = context.getMessageContext();
    HttpServletRequest request = (HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
    String ip=request.getRemoteAddr();
   
    System.out.println(request.getRemoteHost());
     System.out.println(ip+"----");
  
  return "Hello " + text;
 }
}

 

 

4  WEB-INFO 下面添加 cxf-servlet.xml 内容

 

<?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:jaxws="http://cxf.apache.org/jaxws"
 xmlns:soap="http://cxf.apache.org/bindings/soap"
 xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd  
http://cxf.apache.org/jaxws  
http://cxf.apache.org/schemas/jaxws.xsd">

 <jaxws:server id="jaxwsService"
  serviceClass="test.com.launch.x431.business.IHelloWorld"
  address="/testHello">
  <jaxws:serviceBean>
   <bean class="test.com.launch.x431.business.HelloWorldImpl" />
  </jaxws:serviceBean>

<!--输入输出拦截器-->
  <jaxws:inInterceptors>
   <bean
    class="org.apache.cxf.interceptor.LoggingInInterceptor" />
  </jaxws:inInterceptors>
  <jaxws:outInterceptors>
   <bean
    class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
  </jaxws:outInterceptors>
 </jaxws:server>

<beans>

 

部署后自动生成wsdl 访问下  就可以看到生成的 wsdl 8080 后是项目名称

 

http://localhost:8080/x431/services/testHello?wsdl

 

 

5 客户端测试

package com.launch.test;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataHandler;
import javax.xml.soap.SOAPException;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.launch.service.IHelloWorld;

public class Test {

 

 

 public static void main(String[] args) throws ParseException,
   UnsupportedOperationException, SOAPException {

  Map<String, Object> props = new HashMap<String, Object>();

//如果是传文件的时候 打开这个  大文件的时候会快很多
  props.put("mtom-enabled", Boolean.TRUE);
  JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
  soapFactoryBean.setAddress("http://localhost:8080/x431/services/testHello");

  soapFactoryBean.setServiceClass(IHelloWorld.class);
  soapFactoryBean.setProperties(props);

  Object o = soapFactoryBean.create();
  IHelloWorld helloService = (IHelloWorld) o;
  String hello= helloService.sayHello(" world");
  System.out.println(hello); 

 

}

}

 

刚开始做的时候,出现很多问题,很多时候都是因为jar包问题引起的 

如 原来我的 cglib-full-2.0.2 出现问题了 换成了 cglib-nodep-2.2.jar

 或者少了 FastInfoset-1.2.7.jar

 有时候cxf 版本不一样也会出现问题 我用的 是 cxf-2.2.5.jar

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值