'Soap Request with Apache Camel
i have a wsdl file hosted on a site (which i can't share), and i want to invoke a soap request through apache-camel framework. I created a maven project, and used the component apache-cxf to compile the .wsdl file and i got all the .java files. Now i defined a CamelContext and a RouteBuilder to send request, but i am not sure if i understand the flow.
This is my 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();
}
}
This is my 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");
}
}
My 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;
}
}
What i am not understanding, is that the .wsdl is hosted on a site but all the tutorial use as cxf-endpoint localhost. Can someone help me? Thank you.
Solution 1:[1]
Are you using spring? Or just a java app? Are you familiar with beans?
@Value() - shall be used for injecting data from application.properties. Define your String URL like
String WSDL_URL = "YOUR_URL".
Or just put address into your 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;
}
}
Also make your route annotated with @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");
}
}
Solution 2:[2]
You can use defined bean instead:
@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;
}
}
and after use it in the route:
.to("cxf:bean:cxfConvertTemp")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrei Gavrilov |
| Solution 2 | Andrei Gavrilov |
