2017-02-14 68 views
0

我不知道如何在Mulesoft中使用Java Transformer。我想基本上採取一個字符串,並將其轉換爲URL,但我無法弄清楚如何使用Java方法或任何東西。Mulesoft Java Transformer

我在做什麼錯?我怎樣才能讓我調用我的方法,或者讓我的方法與我想要的值一起使用?

ERRORS:

org.mule.module.launcher.DeploymentInitException: IllegalStateException異常:無法轉換類型的值 [transformers.StringTransformer]所需類型 [org.mule.api.processor .MessageProcessor用於屬性 'messageProcessors [4]':沒有匹配的編輯器或轉換策略 發現

所致:org.mule.api.config.ConfigurationException:錯誤創建 名稱爲'streamMigrateAccountToCustomer'的bean:初始化爲 bean失敗;嵌套異常是 org.springframework.beans.ConversionNotSupportedException:無法將 轉換爲屬性'messageProcessors'的類型爲[java.util.ArrayList]的屬性值爲所需類型 [java.util.List];嵌套異常是 java.lang.IllegalStateException:無法將類型 [transformers.StringTransformer]的值轉換爲所需的類型 [org.mule.api.processor.MessageProcessor]屬性 'messageProcessors [4]':沒有匹配的編輯器或轉換戰略 實測值(org.mule.api.lifecycle.InitialisationException) (org.mule.api.config.ConfigurationException)

StringTransformer

@ContainsTransformerMethods 
public class StringTransformer 
{ 
    public String website; 

    public String getWebsite() { 
     return website; 
    } 

    public void setWebsite(String website) { 
     this.website = website; 
    } 

@Transformer 
    public String stringToURL(String string) throws MalformedURLException 
    { 
     return new java.net.URL(string).toString(); 
    } 
} 

XML

<custom-transformer class="transformers.StringTransformer" doc:name="Java"> 
    <spring:property name="website" value="#[payload.Website]"/> 
</custom-transformer> 

輸入到JAVA變壓器

%dw 1.0 
%output application/java 
--- 
{ 
    Id: payload.Id, 
    Name: payload.Name, 
    Active_CMRR__c: payload.Active_CMRR__c, 
    BillingStreet: payload.BillingStreet, 
    BillingCity: payload.BillingCity, 
    BillingState: payload.BillingState, 
    BillingPostalCode: payload.BillingPostalCode, 
    BillingCountry: payload.BillingCountry, 
    OwnerId: payload.OwnerId, 
    Website: payload.Website, 
    Contract_End_Date__c: payload.Contract_End_Date__c, 
    NS_Account_Number__c: payload.NS_Account_Number__c, 
    Phone: payload.Phone, 
    Subscription_Start_Date__c: payload.Subscription_Start_Date__c, 
    NS_Account_Name__c: payload.NS_Account_Name__c, 
    type: payload.type, 
    Owner: { 
     NS_Account_Manager_Id__c: payload.Owner.NS_Account_Manager_Id__c, 
     Id: payload.Owner.Id, 
     type: payload.Owner.type, 
     Name: payload.Owner.Name 
    } 
} 

回答

3

對於一個簡單的字符串操作我更喜歡使用一個簡單的MEL表達,繼承人創建一個新的變量的例子。

<set-variable variableName="website" value="#[new java.net.URL(payload.Website).toString()]" doc:name="Variable"/> 

要在MuleMessage做java transformations,我寧願延長AbstractMessageTransformer類。

public class ExampleAbstractMessageTransformer extends AbstractMessageTransformer { 

@Override 
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { 
    // TODO Auto-generated method stub 
    HashMap<String, String> payload = new HashMap<String, String>(); 
    String websiteUrl = new String(); 

    //Grab Payload 
    payload = (HashMap<String, String>) message.getPayload(); 
    try { 
     websiteUrl = stringToURL(payload.get("Website").toString()); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 


    message.setInvocationProperty("websiteUrl", websiteUrl); 
    return message; 
} 

public String stringToURL(String string) throws MalformedURLException { 
    return new java.net.URL(string).toString(); 
} 
} 

在Mulesoft流內實現。假設java代碼打包爲com.stackoverflow.transformers使用「custom-transformer」組件。

<custom-transformer class="com.stackoverflow.transformers.ExampleAbstractMessageTransformer" doc:name="Java" /> 
0
@ContainsTransformerMethods // since Mule 3.0.1 
    public class MyTransformers {   
  @Transformer   
  public URL stringToURL(String string) {   
     return new java.net.URL(string); 
    } 
    @Transformer 
    public List<URL> stringsToURLs(String string) {     
     List<URL urls = new ArrayList<URL>();    
     for (StringTokenizer tokenizer = new StringTokenizer(string); tokenizer.hasMoreTokens();) {        
      urls.add(new URL(tokenizer.nextToken())); 
     }    
     return urls; 
    } 
} 
+0

試着解釋你的答案。不要只把代碼作爲答案 –