SpringCloud集成cxf步骤记录
1.0 什么是cxf
Apache CXF
是一个开源的Service
框架,简化用户的service
开发,基于CXF开发的应用可提供SOAP
、XML/HTTP
、RESTFUL HTTP
或CORBA
等服务。CXF
底层页可以使用不同的传输协议,包括HTTP
、JMS
或JBI
等。
1.0.1cxf特性
- 支持大量的Web Service标准:包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。
- 支持大量的前端(frontend)编程模型。CXF实现了标准的JAX-WS API,它也包括一种被称为简单前端(simple frontend)的模型,这种模型无需annotation支持。
- CXF支持web service的两种开发模式:
- 规则(contract)优先: 通过编写WSDL来开发
web service
; - 代码优先: 通过编写java代码来开发
webservice.
- 规则(contract)优先: 通过编写WSDL来开发
2. 0 集成引入
<!-- cxf start -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
</dependency>
<!-- cxf end -->
注意事项:
cxf
版本需要与SpringBoot的版本对应,不然因版本问题, 整合cxf-spring-boot-starter-jaxws的启动项目会出现异常。
具体对应关系上仓库进行查看下
https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws查找到jaxws的各种版本,进入之后可以看到对应的springboot版本
image.png
3.服务端
3.1 CXF配置类
/**
* <b>功能描述:CXF配置类</b><br>
*
* @author newzhong
* @version 1.0.0
* @since JDK 1.8
*/
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus bus = new SpringBus();
bus.getFeatures().add(new LoggingFeature());
return bus;
}
}
3.2 服务的发布
自定义注解标注要发布的服务类,发布出去
/**
* <p>description:自动发布接口地址注解</p>
*
* @author newzhong
* @version 1.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoPublish {
/**
*<p>description:发布地址</p>
* @return String
* @author newzhong
*/
String publishAddress();
}
@Component
@Slf4j
public class PublishEndpoint implements ApplicationRunner{
@Autowired
private WebApplicationContext applicationConnect;
@Autowired()
@Qualifier(Bus.DEFAULT_BUS_ID)
private SpringBus bus;
@SuppressWarnings("resource")
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
log.info("开始进行自动发布webService接口");
String[] beanNames = applicationConnect.getBeanNamesForAnnotation(AutoPublish.class);
for(String beanName : beanNames) {
String publishAddr = applicationConnect.getType(beanName).getAnnotation(AutoPublish.class).publishAddress();
EndpointImpl endpoint = new EndpointImpl(bus, applicationConnect.getBean(beanName));
endpoint.publish(publishAddress);
log.info(String.format("发布接口地址:[%s]", publishAddress));
}
log.info("weBservice接口自动发布结束");
}
}
3.3 发布的地址yml,nacos配置
cxf:
path: /cxf
原本默认的默认端口号后 +拼接 /service,现在可以根据需求进行修改
3.4. 新增服务接口
在接口上添加@WebService
注解
-
@WebParam
表示方法的参数,如果不加此注解,方法的参数都从arg0
,开始,随着参数增多,name不断增加为arg1,arg2..........; -
@WebResul
t表示方法的返回值, 没有此注解 返回值名字为return
/**
* <b>功能描述:webService测试接口</b><br>
* @author newzhong
* @version 1.0.0
* @since JDK 1.8
*
* @Note
* <b>创建时间:</b> 2021-03-27 14:32
*/
@WebService(
endpointInterface = "com.newzhong.IWebServiceTest",
serviceName = "WebServiceTest")
@AutoPublish(publishAddr = "test")
public interface IWebServiceTest {
/**
* <b>功能描述:webService测试接口</b>
* @author newzhong
* @version 1.0.0
* @since JDK 1.8
*
* @param name 名字
* @return String
* @Note
*/
String getUserService(@WebParam(name = "name")String name);
}
3.5 新增服务接口实现类
serviceName
: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service。缺省值为 Java 类的简单名称 + Service。(字符串)endpointInterface
: 服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口name
:此属性的值包含XML Web Service的名称。在默认情况下,该值是实现XML Web Service的类的名称,wsdl:portType 的名称。缺省值为 Java 类或接口的非限定名称。(字符串portName
: wsdl:portName。缺省值为 WebService.name+Port。targetNamespace
:指定你想要的名称空间,认是使用接口实现类的包名的反缀wsdlLocation
:指定用于定义 Web Service 的 WSDL 文档的 Web 地址。Web 地址可以是相对路径或绝对路径。(字符串)
注意:实现类上可以不添加Webservice注解 ,加在接口上
@Component
@Slf4j
public class WebServiceTest implements IWebServiceTest{
@Override
public String getUserService(String name) {
log.info("i am",name )
return str;
}
}
3.6 验证服务发布
通过浏览器访问wsdl,wsdl路径即为发布的路径加上?wsdl
http://127.0.0.1:[端口号]/cxf/test?wsdl
可以看到接口就成功了。
到此服务端开发结束!
4.客户端
4.1 IDEA根据?wsdl
生成客户端
- 填入对应的?wsdl
4.2 测试工具跟据?wsdl 测试
如果是soapui工具,发送的参数包括xml的:将xml写在<![CDATA[ 【xml】]]>
里面