[英]Soap Request with Apache Camel
我在网站上托管了一个 wsdl 文件(我无法共享),我想通过 apache-camel 框架调用 soap 请求。 我创建了一个 maven 项目,并使用组件 apache-cxf 编译了 .wsdl 文件,我得到了所有的 .java 文件。 现在我定义了一个 CamelContext 和一个 RouteBuilder 来发送请求,但我不确定我是否理解流程。
这是我的 CamelContext:
/**
* A Camel Application
*/
public class MainApp {
private static final long DURATION_MILIS = 1000;
/**
* A main() so we can easily run these routing rules in our IDE
*/
public static void main(String... args) throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new MyRouteBuilder());
System.out.println("================== STARTING ==================");
camelContext.start();
Thread.sleep(DURATION_MILIS);
System.out.println("================== CLOSING ==================");
camelContext.stop();
}
}
这是我的 RouteBuilder:
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.springframework.stereotype.Component;
@Component
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer://start?repeatCount=1")
.setBody(constant("11"))
.bean(NumberToWordsRequestBuilder.class)
.log("OPERATION_NAME: "+CxfConstants.OPERATION_NAME)
.setHeader(CxfConstants.OPERATION_NAME, constant("NumberToWords"))
.log("OPERATION_NAMESPACE: "+CxfConstants.OPERATION_NAMESPACE)
.setHeader(CxfConstants.OPERATION_NAMESPACE, constant("http://www.dataaccess.com/webservicesserver/"))
.to("cxf:bean:cxfConvertTemp")
//or you can use .to("cxf://someAddress[?options]")
// You can retrieve fields from the response using the Simple language
.log("The title is: ${body[0].book.title}")
.to("mock:output");
}
}
我的 CxfEndpoint class:
import org.apache.camel.component.cxf.CxfEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.apache.test.NumberConversion.NumberConversionSoapType;
@Configuration
public class CxfBeans {
@Bean(name = "cxfConvertTemp")
public CxfEndpoint buildCxfEndpoint() {
CxfEndpoint cxf = new CxfEndpoint();
cxf.setAddress("https://www.dataaccess.com/webservicesserver/NumberConversion.wso");
cxf.setServiceClass(NumberConversionSoapType.class);
cxf.setWsdlURL("https://www.dataaccess.com/webservicesserver/NumberConversion.wso?WSDL");
return cxf;
}
}
我不明白的是,.wsdl 托管在一个站点上,但所有教程都用作 cxf-endpoint 本地主机。 有人能帮我吗? 谢谢你。
您使用的是 spring 吗? 或者只是一个 java 应用程序? 你熟悉豆子吗?
@Value() - 应用于从 application.properties 注入数据。 像这样定义你的字符串 URL
String WSDL_URL = "YOUR_URL".
或者只是将地址放入您的 bean 中:
@Configuration
public class CxfBeans {
@Bean(name = "cxfConvertTemp")
public CxfEndpoint buildCxfEndpoint() {
CxfEndpoint cxf = new CxfEndpoint();
cxf.setAddress("YOUR URL STRING");
cxf.setServiceClass(TempConverterEndpoint.class);
cxf.setWsdlURL("YOUR URL STRING");
return cxf;
}
}
还要使您的路由带有 @Component 注释。
@Componet
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer://start?repeatCount=1")
.setBody(constant("11"))
.bean(NumberToWordsRequestBuilder.class)
.log("OPERATION_NAME: "+CxfConstants.OPERATION_NAME)
.setHeader(CxfConstants.OPERATION_NAME, constant("NumberToWords"))
.log("OPERATION_NAMESPACE: "+CxfConstants.OPERATION_NAMESPACE)
.setHeader(CxfConstants.OPERATION_NAMESPACE, constant("http://www.dataaccess.com/webservicesserver/"))
.to("cxf:bean:cxfConvertTemp")
//or you can use .to("cxf://someAddress[?options]")
// You can retrieve fields from the response using the Simple language
.log("The title is: ${body[0].book.title}")
.to("mock:output");
}
}
您可以改用定义的 bean:
@Configuration
public class CxfBeans {
@Value("${endpoint.wsdl}") //url will be defined in application.properties - your wsdl url address (not localhost)
private String SOAP_URL;
@Bean(name = "cxfConvertTemp")
public CxfEndpoint buildCxfEndpoint() {
CxfEndpoint cxf = new CxfEndpoint();
cxf.setAddress(SOAP_URL);
//service class of your wsdl
cxf.setServiceClass(TempConverterEndpoint.class);
return cxf;
}
}
在路线中使用它之后:
.to("cxf:bean:cxfConvertTemp")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.