【SpringBoot学习】SpringBoot+SpringCXF做WebService接口开发

我只是做个整理而已,由于今天在找如何配置WebService接口拦截路径的配置方式时,大多数都是在application.yml文件中添加配置,但是我想在java代码中进行拦截,所以才有下文

SpringBoot+SpringCXF

SpringBoot版本:SpringBoot 2.2.2.RELEASE
Spring-CXF插件版本:SpringCXF 3.3.3

配置类

第一种

CxfConfig.java

import com.xx.cxf.service.CommonService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

/**
 * @Date: 2020/6/10 22:30
 * @Email: sssrrr879@126.com
 * @Description:
 */
@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;

    @Autowired
    CommonService commonService;

    @Bean(name = "cxfServlet")
    public ServletRegistrationBean<CXFServlet> dispatcherServlet(){
        return new ServletRegistrationBean<>(new CXFServlet(),"/services/*" );
    }

    /** JAX-WS **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, commonService);
        endpoint.publish("/rating");
        return endpoint;
    }
}

第二种

import com.xx.cxf.service.CommonService;
import com.xx.cxf.service.impl.CommonServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

/**
 * @Date: 2020/6/10 22:30
 * @Email: sssrrr879@126.com
 * @Description:
 */
@Configuration
public class CxfConfig {


  @Bean(name = Bus.DEFAULT_BUS_ID)
  public SpringBus springBus()
  {
    return  new SpringBus();
  }


  @Bean
  public CommonService commonService()
  {
    return  new CommonServiceImpl();
  }

  @Bean(name = "cxfServlet")
  public ServletRegistrationBean<CXFServlet> dispatcherServlet(){
    return new ServletRegistrationBean<>(new CXFServlet(),"/services/*" );
  }

  /** JAX-WS **/
  @Bean
  public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), commonService());
    endpoint.publish("/rating");
    return endpoint;
  }
}

配置方式

其中拦截路径配置方式:

@Bean(name = "cxfServlet")
public ServletRegistrationBean<CXFServlet> dispatcherServlet(){
    return new ServletRegistrationBean<>(new CXFServlet(),"/services/*" );
}

可以在application.yml文件中配置为

cxf:
  path: /services

定义接口

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

/**
 * @Date: 2020/6/10 22:34
 * @Email: sssrrr879@126.com
 * @Description:
 */
@WebService(name = "CommonService", // 暴露服务名称
    targetNamespace = "http://service.cxf.xx.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
  @WebMethod
  @WebResult(name = "String", targetNamespace = "")
  String sayHello(@WebParam(name = "userName") String name);

}

定义实现类

import com.xx.cxf.service.CommonService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
 * @Date: 2020/6/10 22:37
 * @Email: sssrrr879@126.com
 * @Description:
 */
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
    targetNamespace = "http://service.cxf.xx.com/", // 与接口中的命名空间一致,一般是接口的包名倒
    endpointInterface = "com.xx.cxf.service.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService {

  @Override
  public String sayHello(String name) {
    return "Hello ," + name;
  }
}

调用客户端

import com.bp.cxf.service.CommonService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Date: 2020/6/10 23:03
 * @Email: sssrrr879@126.com
 * @Description:
 */
public class CxfClient {

  public static String address = "http://localhost:9999/services/rating?wsdl";
  public static void main(String[] args) {
    cl1();
  }

  /**
   * 方式1.代理类工厂的方式,需要拿到对方的接口
   */
  public static void cl1() {
    try {
      // 代理工厂
      JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
      // 设置代理地址
      jaxWsProxyFactoryBean.setAddress( address );
      // 设置接口类型
      jaxWsProxyFactoryBean.setServiceClass( CommonService.class );
      // 创建一个代理接口实现
      CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
      // 数据准备
      String userName = "Leftso";
      // 调用代理接口的方法调用并返回结果
      String result = cs.sayHello( userName );
      System.out.println( "返回结果:" + result );
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 动态调用方式
   */
  public static void cl2() {
    // 创建动态客户端
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient( address );
    // 需要密码的情况需要加上用户名和密码
    // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
    // PASS_WORD));
    Object[] objects = new Object[0];
    try {
      // invoke("方法名",参数1,参数2,参数3....);
      objects = client.invoke( "sayHello", "Leftso" );
      System.out.println( "返回数据:" + objects[0] );
    } catch (java.lang.Exception e) {
      e.printStackTrace();
    }
  }
}

调用地址

http://localhost:9999/services/rating?wsdl
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值