当前位置: 首页 > article >正文

springboot集成cxf

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.syx</groupId>
    <artifactId>cxf-learn</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>3.2.4</cxf.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- webService-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies>

</project>
package com.cxf.config;

import com.cxf.endpoint.Service;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.Collections;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Author whh
 * Date 2023/12/07/ 22:39
 * <p></p>
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private SpringBus bus;

    @Autowired
    private Service service;
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus,service);
        endpoint.setInInterceptors(Collections.singletonList(new LoggingInInterceptor()));
        endpoint.setOutInterceptors(Collections.singletonList(new LoggingOutInterceptor()));

        //将serviceName作为线程池前缀
        WebService annotation = service.getClass().getAnnotation(WebService.class);
        String prefix = annotation.serviceName();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                50,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new BasicThreadFactory.Builder().namingPattern(prefix+"-thread-pool-%d").daemon(true).build(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        //设置线程池
        endpoint.setExecutor(executor);
        endpoint.publish("/api");
        return endpoint;
    }
}

package com.cxf.endpoint;

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

/**
 * Author whh
 * Date 2023/12/07/ 22:22
 * <p></p>
 */
@WebService(targetNamespace = "http://com.cxf.endpoint.Service")
public interface Service {
    @WebMethod
    String sayHello(@WebParam(name = "name")String name);
}

package com.cxf.endpoint;

import org.springframework.stereotype.Component;

import javax.jws.WebService;

/**
 * Author whh
 * Date 2023/12/07/ 22:35
 * <p></p>
 */

@WebService(serviceName = "Service",
        targetNamespace = "http://com.cxf.endpoint.Service",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface = "com.cxf.endpoint.Service")
@Component
public class ServiceImpl implements Service {
    @Override
    public String sayHello(String name) {

        System.out.println(Thread.currentThread().getName());
        return "Hello,"+name;
    }
}

import com.cxf.endpoint.Service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

/**
 * Author whh
 * Date 2023/12/07/ 22:58
 * <p></p>
 */
public class MainTest {


    public static void main(String[] args) {

        String address = "http://127.0.0.1:8080/primal/cxf/api/Service";
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(Service.class);
        // 创建一个代理接口实现
        Service xmlEndPoint = (Service) jaxWsProxyFactoryBean.create();

        Client proxy = ClientProxy.getClient(xmlEndPoint);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(5000);
        policy.setReceiveTimeout(5000);
        conduit.setClient(policy);

        String service = xmlEndPoint.sayHello("123");

        System.out.println(service);

    }
}

server:
  servlet:
    context-path: /primal
  port: 8080



cxf:
  path: /cxf

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
     <soap:Body>
          <ns2:sayHello xmlns:ns2="http://com.cxf.endpoint.Service">
               <name>123</name>
          </ns2:sayHello>
     </soap:Body>
</soap:Envelope>

http://www.kler.cn/a/161629.html

相关文章:

  • 分布式锁实践方案
  • React Hooks在现代前端开发中的应用
  • vscode远程连接服务器并启用tmux挂载进程
  • 将Excel文件的两个表格经过验证后分别读取到Excel表和数据库
  • 代码修改材质参数
  • 大数据新视界 -- 大数据大厂之 Impala 性能飞跃:动态分区调整的策略与方法(上)(21 / 30)
  • 【开源】基于JAVA的个人健康管理系统
  • 华为配置Smart Link负载分担示例
  • Hadoop3.x完全分布式环境搭建Zookeeper和Hbase
  • QT----Visual Studio打开.ui文件报错无法打开
  • debian11,debian 如何删除虚拟内存,交换分区
  • 【Https】HTTPS协议 的介绍及作用
  • Sql Server关于表的建立、修改、删除
  • Appium 并行测试多个设备
  • 【.NET Core】Linq查询运算符(二)
  • 5组10个共50个音频可视化效果PR音乐视频制作模板
  • 制作一个RISC-V的操作系统五-RISC-V汇编语言编程二
  • docker build构建报错:shim error: docker-runc not installed on system
  • 利用 Python 进行数据分析实验(五)
  • 基于微信小程序的智慧校园导航系统研究
  • 2. PyTorch——Tensor和Numpy
  • SpringBoot框架+原生HTML开发,基于云端SaaS服务方式的电子病历编辑器源码
  • Python源码分享10:使用海龟画图turtle画哆啦A梦
  • 微信小程序 - 文件工具类 fileUtil.js
  • 基于Hadoop技术的计算机专业画像平台的设计与研究
  • 数据清洗、特征工程和数据可视化、数据挖掘与建模的主要内容