簡體   English   中英

Soap 請求與 Apache 駱駝

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM