Record the SpringCloud integration CXF steps
1.0 What is CXF
Apache CXF is an open source Service framework that simplifies Service development. Applications developed based on CXF can provide services such as SOAP, XML/HTTP, RESTFUL HTTP, and CORBA. CXF underlying pages can use different transport protocols, including HTTP, JMS, or JBI.
CXF 1.0.1features
- Support for a number of Web Service standards: including SOAP, WS-I Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, and WS-Security.
- Support for a large number of frontend programming models. CXF implements the standard JAX-WS API, and it also includes a model called simple frontend, which requires no annotation support.
- CXF supports two development modes of Web Services:
- Contract first: Develop by writing WSDL
web service
; - Code first: Develop by writing Java code
webservice.
- Contract first: Develop by writing WSDL
2. 0 Integration import
<! -- 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 -->
Copy the code
Note: The CXF version must be consistent with the SpringBoot version. Otherwise, the cxF-Spring-boot-starter-JAXWS startup project integrated with the SpringBoot version may be abnormal.
Check the specific relationship in the warehouse
Mvnrepository.com/artifact/or…
3. The service side
3.1 CXF Configuration Classes
/** * <b> CXF configuration </b><br> **@author newzhong
* @version 1.0.0
* @sinceJDK 1.8 * /
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(a) {
SpringBus bus = new SpringBus();
bus.getFeatures().add(new LoggingFeature());
returnbus; }}Copy the code
3.2 Publishing services
Custom annotations annotate the service classes to be published and published
/** * <p>description: automatically publish the interface address annotation </p> **@author newzhong
* @version1.0 * /
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoPublish {
/** *<p>description: address </p> *@return String
* @author newzhong
*/
String publishAddress(a);
}
Copy the code
@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("Start automatic publishing webService interface");
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("Interface address: [%s]", publishAddress));
}
log.info("Automatic publishing of weBservice interface ends"); }}Copy the code
3.3 Published address yML, nacOS configuration
cxf:
path: /cxf
Copy the code
The default port number followed by + splice /service can now be modified as required
3.4. Added service interfaces
Add @webService annotations to the interface
@WebParam
Represents the parameters of a method. Without this annotation, the parameters of a method are all fromarg0
At first, as parameters increase, name increases to arg1,arg2………. ;@WebResul
T represents the return value of the method. Without this annotation, the return value is namedreturn
/** * <b> <br> *@author newzhong
* @version 1.0.0
* @since JDK 1.8
*
* @Note* <b> create time :</b> 2021-03-27 14:32 */
@WebService( endpointInterface = "com.newzhong.IWebServiceTest", serviceName = "WebServiceTest")
@AutoPublish(publishAddr = "test")
public interface IWebServiceTest {
Function description :webService test interface </b> *@author newzhong
* @version 1.0.0
* @since JDK 1.8
*
* @paramThe name name *@return String
* @Note* /
String getUserService(@WebParam(name = "name")String name);
}
Copy the code
3.5 Added service Interface implementation classes
-
ServiceName: specifies the name of the published Service. WSDL: Service specifies the name of the Web Service. The default value is the simple name of the Java class + Service. (String)
-
EndpointInterface: specifies the full path of the Service Interface. It specifies the Service EndPoint Interface (SEI)
-
Name: The value of this attribute contains the name of the XML Web Service. By default, this value is the name of the class implementing the XML Web Service, WSDL: the name of the portType. The default value is the unqualified name of a Java class or interface. (string
-
PortName: WSDL: portName. The default value is webservice. name+Port.
-
TargetNamespace: Specifies the namespace you want, as the reverse of the package name that uses the interface implementation class
-
WsdlLocation: Specifies the Web address of the WSDL document used to define the Web Service. The Web address can be a relative path or an absolute path. (String)
Note: You can add Webservice annotations to the interface without adding them to the implementation class
@Component
@Slf4j
public class WebServiceTest implements IWebServiceTest{
@Override
public String getUserService(String name) {
log.info("i am"Name),returnstr; }}Copy the code
3.6 Verifying service Publishing
Access the WSDL through a browser, and the WSDL path is the published path plus? wsdl
http://127.0.0.1:[port number]/ CXF /test? WSDL
You can see that the interface is successful.
End of this server development!
4. The client
4.1 the IDEA according to? wsdl
Generate client
- Fill in the corresponding? wsdl
4.2 Test Tool Tracking? WSDL test
In the case of the SOAPUI tool, the parameters sent include XML: write the XML to