I am trying to invoke a java function in mule.I converted the payload into the Object and passed it in the function.
The name of the Java object created is req.
The method validate accepts a Java Object Of type Example
public HashMap<String, String> validate(Example req) {.......}
Example class looks like this:
Class Example{
String key1;
String key2;
String key3;
}
XML configuration looks like this :
<java:new constructor="Example(java.lang.String,java.lang.String,java.lang.String)" doc:name="New Example" doc:id="6a1d5c8c-a1f0-446e-ab49-99a21fbbf4b9" class="Entities.Example" target="req">
<java:args ><![CDATA[#[{key1 :payload.key1,key2: payload.key2, key3:payload.key3}]]]></java:args>
</java:new>
<java:invoke doc:name="Invoke" doc:id="dd5f6534-06c8-4f4d-b3aa-c634a629898e" class="Implementations.ValidationServiceImpl" instance="#[vars.validator]" method="validate(Entities.Example)">
<java:args ><![CDATA[#[vars.req]]]></java:args>
</java:invoke>
I get the following error:
I don't know why is it not passing the java object as a whole.
Please try with this and see. If it works for you please let me know. We need to improve the mule documentation in this case
<java:args >#[{req: vars.req}]</java:args>
Related
I am trying to write a generic method for deserializing json into my model. My problem is that I don't know how to get Class from the generic type T. My code looks something like this (and doesn't compile this way)
public class JsonHelper {
public <T> T Deserialize(String json)
{
Gson gson = new Gson();
return gson.fromJson(json, Class<T>);
}
}
I tried something else, to get the type, but it throws an error I had the class as JsonHelper<T> and then tried this
Class<T> persistentClass = (Class<T>) ((ParameterizedType)getClass()
.getGenericSuperclass())
.getActualTypeArguments()[0];
The method signature looks like this
com.google.gson.Gson.fromJson(String json, Class<T> classOfT)
So, how can I translate along T so that when I call JsonHelper.Deserialize<MyObject>(json); I get an instance of the correct object?
You need to get a Class instance from somewhere. That is, your Deserialize() method needs to take a Class<T> as a parameter, just like the underlying fromJson() method.
Your method signature should look like Gson's:
<T> T Deserialize(String json, Class<T> type) ...
Your calls will look like this:
MyObject obj = helper.Deserialize(json, MyObject.class);
By the way, the convention to start method names with a lowercase letter is well established in Java.
Unfortunately, the way Java handles generics, you cannot get the class like you're asking. That's why Google's stuff asks specifically for the class as an argument. You'll have to modify your method signature to do the same.
I have a class in java:
public class MyClass{
public String a=null;
public String b=null;
public String c=null;
public String d=null;
public static String testMyClass() {
//my method instruction using the attributes a,b,c and d
}
The trouble is that MyClass attributes must be received from python to be able to use the method testMyClass. So is it possible to instantiate a java class from python and after send it back to java using py4j?
Anyone can show how to make it if it is possible? Or have another alternative?
I don't think that is possible unless someone knows of a "bridge" utility between Java and Python.
However, you may do this:
Create a JSON in the Python script in a structure that matches that of MyClass
Receive it in your Java method and deserialize the JSON into an instance of MyClass using Jackson or some JSON tool.
Your next problem will be the communication between you Java process and Python process. For that, see if this StackOverflow question helps: Passing data from Java process to a python script
We are able to call java in C# code and able to pass Strings and integers as method parameters. Able to access Strings are integers directly by using java java.lang.String and java.lang.Integer with out issues.
But when we pass C# custom object as method parameter we could not find a way to access it.
Eg: Employee.cs is the C# class with parameter name.
Used proxygen to create EmployeeLibraray.dll, EmployeeLibraray.j4n.dll and EmployeeLibraray.j4n.
C# code where we are trying to pass C# object
var bs = new BridgeSetup();
bs.AddAllJarsClassPath("./");
Bridge.CreateJVM(bs);
Bridge.RegisterAssembly(typeof(JavaTest).Assembly);
JavaTest obj = new JavaTest(); ================================> JavaTest
is the class generated using proxygen where it is called in C#
EmployeeLibraray.Employee e = new EmployeeLibraray.Employee();
===========> Custom C# object to be passed to Java
e.LoginName = "test";
obj.execute( e);
Code in java
public void execute(system.Object inputObj) throws Exception {
}
Question
How should we cast the inputObj as Employee so that can access directly e.getName().
I'm working with Play framework via a Java project and I'd like to pass my templates (Scala functions) as parameters to one of my Java method.
I'd like to do something like this :
public static Result ok(ScalaFunction template, Object obj) {
// do some work, then :
return ok(template.render(obj));
}
MyClass.ok(views.html.mytemplate, SomeModel.find.findList());
Of course, this doesn't work. I supposed views.html.mytemplate is a class, so I switched to views.html.mytemplate.class and public static Result ok(Class template, Object obj) in my method, but I can't call render on it.
Is it possible to do something like this ?
If someone knows a better alternative, what I'm trying to achieve is either return a JSON representation of obj if the Accept header is "application/json", or the compiled template (given in first parameter) if the Accept header is "text/html".
Say your Scala function takes and returns an Object, hence of type Function1<Object, Object> then your Java method should look like this:
public Result ok(Function1<Object, Object> template, Object obj) {
// do some work, then:
return ok(template.render(obj));
}
I'm trying to write a data access layer for an AJAX web project. This DAL has to convert data coming in via an AJAX servlet to objects that can be passed to a PreparedStatement for execution.
Data in the AJAX servlet, retrieved by using HttpServletRequest.getParameter(...), come in as strings.
In each data class, I have a known set of fields as well as their data types, e.g. CustomerId(integer), CustomerName(string).
I can of course write a method in the Customer class to handle the conversion, but this means I have to do it for every data object's class. I would much rather have a generic method that does conversion, e.g.
Object convert(String value, Class<?> targetType) { ... }
Can anyone point me in the right direction?
Create an utility class with all conversion methods you would like to use. Inside its static initializer, make use of reflection to collect all those methods by parameter type and return type in a map. Then, in the convert() method just pick the method which suits the given source and target type and invoke it. Make use of generics to fix the return type to be the same as the target type:
public static <T> T convert(Object from, Class<T> to)
You can find an example in this article.
But as bmargulies pointed out, JSON is also an interesting option. You could let ajax to send all parameters as one JSON string. Then, you can use a JSON-to-Javabean converter like Google Gson to convert the JSON string to a fullworthy Javabean like Customer. It'll be as simple as:
String jsondata = request.getParameter("jsondata");
Customer customer = new Gson().fromJson(jsondata, Customer.class);
// ...
See also this answer for another example.
There are JSON libraries that will do data type conversion. Jackson is one. Or, you could code the whole think using a JAX-RS service framework instead of a raw servlet, and it will take care of all this for you. Apache CXF is one framework that contains this support. Since you are asking for a generic solution, why not use one that's already out there.
We do this exact thing using a plethora of static converters in a utility class. It isn't elegant but it sure is easy and effective.
class Util {
public static Long StringToLong(String s) { ... }
public static Integer StringToInt(String s) { ... }
public static Date StringToDate(String s) { ... }
public static Date StringToDateYYMMDD(String s) { ... }
public static BigDecimal StringToBigDecimal(String s) { ... }
// etc ad naseum
}
Since you want to use the parameters in your PreparedStatement, why do you have to convert them at all?
When using setString(index, parameter) SQL will be happy to do the conversion for you.
Thus the only thing you might want to do is some kind of validation that the input is really valid (or you could even leave this part to your SQL engine which will throw an exception if it doesn't understand you.