我不知道如何在Mulesoft中使用Java Transformer。我想基本上采用String并将其转换为URL,但我无法弄清楚如何使用Java方法或任何东西。
我做错了什么?我怎样才能调用我的方法,或者使我的方法与我想要的值一起使用?
错误:
org.mule.module.launcher.DeploymentInitException: IllegalStateException:无法转换类型的值 [transformers.StringTransformer]到必需的类型 [org.mule.api.processor.MessageProcessor]属性 'messageProcessors [4]':没有匹配的编辑器或转换策略 结果
引起:org.mule.api.config.ConfigurationException:创建错误 名为'streamMigrateAccountToCustomer'的bean:初始化 豆子失败了;嵌套异常是 org.springframework.beans.ConversionNotSupportedException:失败 将[java.util.ArrayList]类型的属性值转换为所需类型 [java.util.List]属性'messageProcessors';嵌套异常是 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 TRANSFORMER
%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
}
}
答案 0 :(得分: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使用&#34; custom-transformer&#34;零件。
<custom-transformer class="com.stackoverflow.transformers.ExampleAbstractMessageTransformer" doc:name="Java" />
答案 1 :(得分:0)
您可以使用invoke组件来调用java方法。检查https://docs.mulesoft.com/mule-user-guide/v/3.7/invoke-component-reference
答案 2 :(得分: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;
}
}