apache cxf使用

  • 依赖包
 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!-- Jetty is needed if you're are not using the CXFServlet -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.12</version>
    </dependency>

JaxWsServerFactory用法

1、父类ServerFactoryBean

不建议使用,建议使用它的子类JaxWsServerFactory

PersonService服务类

使用这个服务类上不需要加@webservice注解

服务类没有服务方法,不报错

缺点是生成的wsdl文档命名不规范

package com.stduy.server;

public class PersonService {
    public String sayHello(String userName){
        return "hello "+userName;
    }
}

发布类

public class Publisher {
    public static void main(String[] args) {
        //创建cxf服务发布对象
        ServerFactoryBean sfb = new ServerFactoryBean();
        //设置服务的类
        sfb.setServiceClass(PersonService.class);
        //设置服务地址
        sfb.setAddress("http://localhost:5555/hello");
        //设置服务类的对象
        sfb.setServiceBean(new PersonService());
        //发布
        sfb.create();
    }
}

发布后在浏览器方法http://localhost:5555/hello?wsdl可以得到wsdl

2、子类JaxWsServerFactory

建议使用子类,不建议使用父类

服务类

服务类必须加@WebService注解,不然发布的时候,就会按照空服务进行发布

@WebService
public class PersonService {
    public String sayHello(String userName){
        return "hello "+userName;
    }
}

发布类

public class Publisher {
    public static void main(String[] args) {
        //创建cxf服务发布对象
        JaxWsServerFactoryBean sfb = new JaxWsServerFactoryBean();
        //设置服务的类
        sfb.setServiceClass(PersonService.class);
        //设置服务地址
        sfb.setAddress("http://localhost:5555/hello");
        //设置服务类的对象
        sfb.setServiceBean(new PersonService());
        //发布
        sfb.create();
    }
}

如果发布带有接口的webservice需要在接口上加@webservice注解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值