Create CXF Client

Create CXF Client Example explains step by step details of how to create a CXF web service client / wsdl soap client using Apache CXF WebService Framework

For Creating Apache CXF Client, We are using org.apache.cxf.jaxws.JaxWsProxyFactoryBean. The factory option is particular to CXF and it is non-standard. This will give you a great control over the endpoint.

The Other options like Wsimport tool available with JDK are standard but that will give you less control. For example you can add a logging interceptor in order to show the logs when accessing your service using org.apache.cxf.jaxws.JaxWsProxyFactoryBean.

For publishing a web service, you can follow this tutorial CXF Web Service Tutorial. This client is based on mentioned Tutorial

Note

For running this client you need to refer the classes created on CXF Web Service Tutorial

You can also read about implementing an Asynchronous client CXF Asynchronous Client

Required Libraries

You need to download following libraries in order to Generate CXF Client

  1. OpenJDK 8 / Corretto
  2. Eclipse 4.15
  3. CXF-3.3.6
  4. Tomcat 9 (Deploying Web Service)

Following jar must be in classpath

  1. cxf-core-3.3.6.jar
  2. cxf-rt-bindings-soap-3.3.6.jar
  3. cxf-rt-databinding-jaxb-3.3.6.jar
  4. cxf-rt-features-logging-3.3.6.jar
  5. cxf-rt-frontend-jaxws-3.3.6.jar
  6. cxf-rt-frontend-simple-3.3.6.jar
  7. cxf-rt-transports-http-3.3.6.jar
  8. cxf-rt-wsdl-3.3.6.jar
  9. httpasyncclient-4.1.4.jar
  10. httpclient-4.5.12.jar
  11. httpcore-4.4.13.jar
  12. httpcore-nio-4.4.13.jar
  13. neethi-3.1.1.jar
  14. slf4j-api-1.7.29.jar
  15. slf4j-jdk14-1.7.29.jar
  16. spring-aop-5.1.14.RELEASE.jar
  17. spring-beans-5.1.14.RELEASE.jar
  18. spring-context-5.1.14.RELEASE.jar
  19. spring-core-5.1.14.RELEASE.jar
  20. spring-expression-5.1.14.RELEASE.jar
  21. spring-jcl-5.1.14.RELEASE.jar
  22. spring-web-5.1.14.RELEASE.jar
  23. stax2-api-3.1.4.jar
  24. woodstox-core-5.0.3.jar
  25. wsdl4j-1.6.3.jar
  26. xmlschema-core-2.2.5.jar

Create CXF Client

import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.student.ChangeStudentDetails;
import com.student.Student;

// CXF JAX-WS Client / Consuming Web Services With CXF

public final class StudentClient {

   
public static void main(String args[]) throws Exception {

     
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

      factory.setServiceClass
(ChangeStudentDetails.class);
      factory.setAddress
("http://localhost:8080/CXFTutorial/ChangeStudent?wsdl");
      factory.getInInterceptors
().add(new LoggingInInterceptor());
      factory.getOutInterceptors
().add(new LoggingOutInterceptor());
      ChangeStudentDetails client =
(ChangeStudentDetails) factory.create();
      Student student =
new Student();
      student.setName
("Rockey");
      Student changeName = client.changeName
(student);
      System.out.println
("Server said: " + changeName.getName());
      System.exit
(0);
   
}
}
Output

Server said: Hello Rockey

 









4 Responses to "Create CXF Client"
  1. lacike 2011-11-09 20:47:01.0
  1. admin 2011-11-11 20:27:01.0
  1. sorty 2011-11-13 20:47:01.0
  1. admin 2011-11-14 12:47:11.0

Your email address will not be published. Required fields are marked *