java - 如何使用 apache cxf 创建 Web 服务客户端?

标签 java web-services soap cxf

我已经关注了这个tutorial创建一个客户端。 此代码是否可以向部署在同一服务器上的另一个 Web 服务发出请求?
找不到任何相关文档或示例:(

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 studenClient = (ChangeStudentDetails) factory.create();
studenClient.setName("Rockey");

// Is this valid code ?
factory.setAddress("http://localhost:8080/CXFTutorial/ChangeTeacher?wsdl");
ChangeTeacherDetails teacherClient = (ChangeTeacherDetails) factory.create();
Teacher teacher = teacherClient.setName("Leonardo");

最佳答案

以下来源有几个工作示例:

https://github.com/apache/cxf/blob/master/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java#L107

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = CachingTest.class.getResource("cxf-client.xml");

    Bus bus = bf.createBus(busFile.toString());
    SpringBusFactory.setDefaultBus(bus);
    SpringBusFactory.setThreadDefaultBus(bus);

    URL wsdl = CachingTest.class.getResource("DoubleIt.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItTransportSAML1Port");
    DoubleItPortType port = 
        service.getPort(portQName, DoubleItPortType.class);
    ((BindingProvider)port).getRequestContext().put("thread.local.request.context", "true");
    updateAddressPort(port, PORT);

    // Make a successful invocation
    doubleIt(port, 25);

    // Change the STSClient so that it can no longer find the STS
    BindingProvider p = (BindingProvider)port;
    clearSTSClient(p, bus);

    // This should succeed as the token is cached
    doubleIt(port, 30);

    // This should fail as the cached token is manually removed
    Client client = ClientProxy.getClient(port);
    Endpoint ep = client.getEndpoint();
    ep.remove(SecurityConstants.TOKEN_ID);
    ep.remove(SecurityConstants.TOKEN);

    try {
        doubleIt(port, 35);
        fail("Expected failure on clearing the cache");
    } catch (SOAPFaultException ex) {
        // Expected
    }

    ((java.io.Closeable)port).close();
    bus.shutdown(true);

关于java - 如何使用 apache cxf 创建 Web 服务客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35848880/

相关文章:

java - 如何将 Firebase 电话身份验证与电子邮件/密码身份验证相关联?

python - Suds + JIRA = SAXException

Java 1.7 com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException : Cannot resolve element with ID data

php - 从本地 LAMP 访问 SoapClient 和 file_get_contents

java - 格式化 Web 服务响应

java - 使用动态规划计算多个值的箱子中有多少球

java - 在 C++ 和 Java 中只有一个返回值的原因是什么?

java - 提取公共(public)列(审核字段)[Java][JPA]

java - 使用 ksoap2 : parameters are all null 从 Android 调用 java webservice

python - 处理(排队)对调用速率受限的外部 API 的 Web 服务的请求