Apache CXF
CXF一个开源的Service框架,它实现了JCP与Web Service中一些重要标准,CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。
CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。
CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。
SpringBoot集成CXF发布WebService
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="#34;
xmlns:xsi="#34;
xsi:schemaLocation=" #34;>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo06</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 统一版本配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-boot.version>2.3.9.RELEASE</spring-boot.version>
</properties>
<!-- 声明SpringBoot依赖版本 -->
<dependencyManagement>
<dependencies>
<!--spring-boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot - Web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot - 健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- CXF webservice -->
<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>6.0.18.Final</version>
</dependency>
</dependencies>
</project>
WebService服务类:
package com.what21.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "userWebService",
// 命名空间,一般是接口的包名倒序
targetNamespace = "#34;
)
public interface UserWebService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
String userInfo(@WebParam(name = "username") String username);
}
package com.what21.demo.service;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
// 与接口中指定的name一致
@WebService(serviceName = "userWebService",
// 与接口中的命名空间一致,一般是接口的包名倒
targetNamespace = "#34;,
// 接口地址
endpointInterface = "com.what21.demo.service.UserWebService"
)
@Component
public class UserWebServiceImpl implements UserWebService {
@Override
public String userInfo(String username) {
System.out.println("username=" + username);
return username;
}
}
配置类:
package com.what21.demo.config;
import com.what21.demo.service.UserWebService;
import org.apache.cxf.Bus;
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.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
private UserWebService userWebService;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, userWebService);
endpoint.publish("/userWebService");
return endpoint;
}
}
启动类:
package com.what21.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.yml
# Tomcat
server:
servlet:
context-path: /demo
tomcat:
uri-encoding: UTF-8
#最小线程数
min-spare-threads: 500
#最大线程数
max-threads: 2500
#最大连接数
max-connections: 5000
#最大等待队列长度
accept-count: 1000
#请求头最大长度kb
max-http-header-size: 1048576
#启动APR(非阻塞IO)
protocol: org.apache.coyote.http11.Http11AprProtocol
#端口
port: 9199
# Spring
spring:
application:
# 应用名称
name: demo-cxf-server
cxf:
path: /cxf
启动后的访问地址
<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:xsd="#34; xmlns:wsdl="#34;
xmlns:tns="#34; xmlns:soap="#34;
xmlns:ns1="#34; name="userWebService"
targetNamespace="#34;>
<wsdl:types>
<xs:schema xmlns:xs="#34; xmlns:tns="#34;
elementFormDefault="unqualified" targetNamespace="#34; version="1.0">
<xs:element name="userInfo" type="tns:userInfo"/>
<xs:element name="userInfoResponse" type="tns:userInfoResponse"/>
<xs:complexType name="userInfo">
<xs:sequence>
<xs:element minOccurs="0" name="username" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="userInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="String" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="userInfoResponse">
<wsdl:part element="tns:userInfoResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="userInfo">
<wsdl:part element="tns:userInfo" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="userWebService">
<wsdl:operation name="userInfo">
<wsdl:input message="tns:userInfo" name="userInfo">
</wsdl:input>
<wsdl:output message="tns:userInfoResponse" name="userInfoResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="userWebServiceSoapBinding" type="tns:userWebService">
<soap:binding style="document" transport="#34;/>
<wsdl:operation name="userInfo">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="userInfo">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="userInfoResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="userWebService">
<wsdl:port binding="tns:userWebServiceSoapBinding" name="UserWebServiceImplPort">
<soap:address location="#34;/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用WebService服务
实现
package com.what21.demo.service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class WebServiceTest {
public static void main(String[] args) {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("#34;);
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("userInfo", "wangyangming");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}