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

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 {@Autowiredprivate SpringBus bus;@Autowiredprivate Service service;@Beanpublic 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 {@WebMethodString 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 {@Overridepublic 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: /primalport: 8080cxf: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.lryc.cn/news/255142.html

相关文章:

  • 快速认识什么是:Kubernetes
  • YOLOv6 学习笔记
  • paypal贝宝怎么绑卡支付
  • 活动回顾|德州仪器嵌入式技术创新发展研讨会(上海站)成功举办,信驰达科技携手TI推动技术创新
  • Vue 循环走马灯
  • <Linux>(极简关键、省时省力)《Linux操作系统原理分析之Linux文件管理(3)》(27)
  • 【华为数据之道学习笔记】3-2 基础数据治理
  • GO设计模式——7、适配器模式(结构型)
  • Java实现TCP一对一通信,实现UDP群聊通信
  • Vue + Element 实现按钮指定间隔时间点击
  • UE Websocket笔记
  • STM32h7 接收各种can id情况下滤波器的配置
  • 《深入理解计算机系统》学习笔记 - 第三课 - 浮点数
  • 总结:服务器批量处理http请求的大致流程
  • 算法通关村第十八关-青铜挑战回溯是怎么回事
  • 区分node,npm,nvm
  • 7-2 小霸王
  • Linux内核上游提交完整流程及示例
  • TS学习——快速入门
  • 深圳锐科达风力发电广播对讲解决方案
  • 极智芯 | 解读国产AI算力 璧仞产品矩阵
  • Echarts折线图常见问题及案例代码
  • javaTCP协议实现一对一聊天
  • 机器学习应用 | 使用 MATLAB 进行异常检测(上)
  • Java -jar参数详解
  • RocksDB 在 vivo 消息推送系统中的实践
  • 【C进阶】C程序是怎么运作的呢?-- 程序环境和预处理(上)
  • 点滴生活记录1
  • gitea仓库迁移
  • 〖大前端 - 基础入门三大核心之JS篇㊽〗- BOM特效开发