WebServiceCXF1

简单的WebServiceCXF配置

 

一、       WebService服务端:

1)       添加以下jar包:

 

2)       添加以下类:

接口ISayHello.java

@WebService   //这个不能少

public interface ISayHello {

       

           public Stringhello(String args);

       

}

 

实现类SayHelloImpl.java

public class SayHelloImp implements ISayHello {

      

           public String hello(Stringargs) {

              return "success!!!" + args;

           }

      

}

 

3)       添加Spring的配置文件applicationContext_cxf.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                     http://www.springframework.org/schema/beans/spring-beans.xsd

                     http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">

 

    <!--导入与CXF框架有关的xml -->

    <import resource="classpath:META-INF/cxf/cxf.xml" />

    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

      

           <!-- 发布WebService接口 -->

    <jaxws:endpoint id="sayHello1"

       implementor="com.mei.service.SayHelloImp"address="/cxfTest" />

 

</beans>

 

4)       修改web.xml文件:

<servlet>

    <servlet-name>WebServiceCXF</servlet-name>

<servlet-class>

org.apache.cxf.transport.servlet.CXFServlet

</servlet-class>

    <load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

    <servlet-name> WebServiceCXF</servlet-name>

    <url-pattern>/services/*</url-pattern>

</servlet-mapping>

 

在地址栏里输入http://localhost:8664/WebServiceCXF/services/,如果出现以下页面则发布成功

 

 

二、       WebService客户端

1)      添加以下jar包:

 

2)      添加接口,类名与服务端类名最好相同(包名可不同):

@WebService    //这个不能少

public interface ISayHello {

public Stringhello(String args);

}

 

3)      添加测试类:

public static void main(String[] args) {

    try {

        String url = "http://localhost:8664/WebServiceCXF/services/cxfTest";

        JaxWsProxyFactoryBean factory= new JaxWsProxyFactoryBean();

        factory.setAddress(url);

        factory.setServiceClass(ISayHello.class);

        factory.create();

        ISayHello result =(ISayHello)factory.create();

        System.out.println(result.hello("WebServiceCXF").toString() + "  -------------");

    } catch (Exception e) {

        e.printStackTrace();

    }

}

 

运行后控制台输出以下信息:

 

注:如果返回的是JavaBeanList,参照文档WebService_CXF2

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值