<?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/> </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><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;
@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()));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;
@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;@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;
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>