[英]How to create web services client with apache cxf?
I have followed this tutorial to create a client. 我已经按照本教程创建了一个客户端。 Is this valid code to make a request to another web service deployed on the same server ? 该有效代码是否可以向部署在同一服务器上的另一个Web服务发出请求?
Could not find any relevant documentation or example :( 找不到任何相关文档或示例:(
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");
The following source has several working examples: 以下资源有几个工作示例:
https://github.com/apache/cxf/blob/master/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts/caching/CachingTest.java#L107 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);
This might be relevant: 这可能是相关的:
public void useWebClient() {
System.out.println("Using WebClient to get the book with id 123");
WebClient client = WebClient.create("http://localhost:" + port + "/services/bookstore/");
Book book = client.post(new Book("HTTP"), Book.class);
System.out.println(book.getId() + ":" + book.getName());
}
public void useJMSClient() throws Exception {
System.out.println("Getting the book with id 123 over JMS");
getBookOverJMS();
System.out.println("Adding a new book over JMS");
addGetBookOverJMS(new Book("JMS Transport"));
System.out.println("Adding a new book over JMS Oneway");
addGetOnewayBookOverJMS();
System.out.println("Adding a new book over HTTP Oneway, "
+ " and getting it back over JMS");
addOnewayOverHttpGetOverJMS();
}
Full source code: 完整的源代码:
http://code.openhub.net/file?fid=0fBMqrwxmzdpDR3J4W1NvMxLnbA&cid=rtwtj2OYF4c&s=How%20to%20create%20web%20services%20client%20with%20apache%20cxf%3F&pp=0&fl=Java&ff=1&filterChecked=true&fp=1511&mp,=1&ml=1&me=1&md=1&projSelected=true#L0 http://code.openhub.net/file?fid=0fBMqrwxmzdpDR3J4W1NvMxLnbA&cid=rtwtj2OYF4c&s=How%20to%20create%20web%20services%20client%20with%20apache%20cxf%3F&pp=1&fl=ed&Check=Java&ff = 1&me = 1&md = 1&projSelected = true#L0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.