Mule : Could you please help following query : In Anypoint Mule IDE, How to assign values to a Payload like this "Value1|Value2|Value3|Value4|.." ? and then assign the payload to Java component in Mule?
I tried following approach:
(1) define Java component and return String object as output (by implementing callable method as below):
public class InputToJavaComponent implements Callable {
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
return "Value1|Value2|Value3";
}
I was checking if is there any other simple way to achieve same thing.
Thanks.
Can you please try like this in set payload and let us know whether it work or not
[flowVars.Value1+'|']#[flowVars.Value2+'|']#[flowVars.Value3+'|']#[flowVars.Value4]
hi i tried as the below way and it was working fine for me, try in this way
[sessionVars.Value1+'|']#[sessionVars.Value2+'|']#[sessionVars.Value3+'|']#[sessionVars.Value4]
Both approaches as follow works fine:
(1)
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
return "Value1|Value2|Value3";
}
(2)
[flowVars.Value1+'|']#[flowVars.Value2+'|']#[flowVars.Value3+'|']#[flowVars.Value4]
Related
I'm using Jax RS 2.0 ContainerRequestFilter
I want to intercept entity body (if any) and convert to original type.
I can get original class type by using inkected ResourceInfo
resinfo.getResourceMethod().getParameters()
However I have no idea on how to get parameter value...
The only closes object is an EntityStream(), available from:
containerRequestContext.getEntityStream()
I guess I should use the above object, but how I can rebuild original object from entityStream ?
I found a way, it is enought to implement ReaderInterceptor interface:
#Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
final Object proceed = readerInterceptorContext.proceed();
return proceed;
}
I am using jaxrs 2.0 & am trying to populate an object in the ContainerRequestContext & retrive it in the business logic.
I am populating the object in the ContainerRequestContext like :
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MyObject myObject = new MyObject();
requestContext.setProperty("myObject", myObject);
}
And I am trying to read it in the business logic like :
#GET
#Path("/custom")
public String data(#Context ContainerRequestContext crc) {
Object pco = crc.getProperty("myObject");
}
I am using this approach from the example shared in : How to inject an object into jersey request context?
However I am getting the ContainerRequestContext crc as null. Can someone tell me what I could be doing wrong ?
I have tested the exact code asked in the question which works as it is with following setup:
spring-boot-starter-parent: 2.3.4.RELEASE
spring-boot-starter: inherited from parent
spring-boot-starter-jersey: inherited from parent
which evaluates to Spring 5.2.9.RELEASE, Jersey 2.30.1
Arquillian with Jersey can work.
I am working on creating a Mule Anypoint connector using DevKit to connect to an internally developed API. Using the #RestCall annotation, I was able to successfully use my connector in a Mule flow, with the connector returning a String of the following JSON:
{
"name": "Bryan",
"email": "bryan#myservice.com",
"uid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true
}
I know I could implement a "JSON to Object" transformer in the Mule flow, but I would like for the connector to return a POJO instead. That being said, I modified abstract method annotated by the #RestCall to return an Account object:
#Processor
#RestCall(
uri="https://api.myservice.com/v2/account",
method=HttpMethod.GET,
contentType = "application/json")
public abstract Account getUserInformation() throws IOException;
Just for reference, here is how I defined Account:
package org.mule.modules.myservice.model;
import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = -1L;
private String name;
private String email;
private String uid;
private boolean emailVerified;
/**
* removed getters and setters for brevity
*/
}
However, now that I am trying to return the Account object, I receive the following error:
org.mule.api.transformer.TransformerException: Could not find a transformer to transform "SimpleDataType{type=java.lang.String, mimeType='/'}" to "SimpleDataType{type=org.mule.modules.myservice.model.Account, mimeType='/'}".
I see that there is a #Transformer annotation, but documentation is lacking on how to implement this in regards to working with JSON and/or working within the context of a #RestCall within a Mule connector.
Can anyone offer up advice on how to transform a String of JSON into an Account object within the connector?
At some point (not where you are pointing at) you are trying to pass an String to an operation that needs Account. Register a transformer for that or transform manually before the operation.
I received the following response in another forum and wanted to pass it along:
In order to use DevKit #RestCall with pojos, you need to define the transformers from String to your POJO.
Check this example: https://github.com/pablocabrera85/json-place-holder-connector
For my specific question, this was the type of example I was looking for and indeed worked.
We have a service component (#service) which internally uses MyBatis to do CRUD operations. All the components are managed by Spring.
Lets assume our MyBatis mapper file
class MyBatisMapper{
public void getData(Map<String,Object> arg);
}
In our mybatis xml file,we have element defined for a callable statement. arg is a map which holds IN and OUT parameters.
Our service component is like
#Service("myService")
class MyService{
#Autowired
private MyBatisMapper myMapper;
public void processData(){
Map<String,Object> input = new HashMap<String,Object>();
............
............
myMapper.getDat(input);
Object response = input.get("RESPONSE");
//process response and so on
}
My objective is to write Junits for the service component and don't have to interact with DB. So, I tried to use JMockIt to mock some of the DAO operations
My attempt is ike
#Mocked
#Injectable
private MyBatisMapper myMapper;
#Test
public void testService() {
new NonStrictExpectations(){
{
Map<String,Object> input = new HashMap<String,Object>();
input.put("xxx,"yyy");
myMapper.getData(input);
}
};
}
But when I ran my tests, I noticed that call went through actualy DB , instead of mocked one.
How to make sure only the mocked components gets called, instead of the actual object
#Injectable mock instances only get injected into #Tested objects. And to specify a return value from a mocked method, you need to assign it to the result field. So, the test should look like the following:
#Tested MyService myService;
#Injectable MyBatisMapper myMapper;
#Test
public void testService() {
final Map<String,Object> data = new HashMap<String, Object>();
data.put("RESPONSE", "yyy");
new Expectations() {{
myMapper.getData((Map<String, Object>) any); result = data;
}};
myService.processData();
// Assert some expected outcome.
}
Thank you Rogerio. I tried to follow the approach you mentioned, but still I see the call going to the actual object. There is a point which I did not mention earlier. My project is spring based and the components are all autowired.
My test has
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring/test-applicationContext.xml"})
and test-applicationContext.xml has component scan. So I think, Spring is injecting the actual object ignoring the mock instruction.
Am I wrong ?
I want to avoid using the Callable interface in a Mule component, instead of this i want to use annotations based coding.
I am using the following code but getting an error.
How can i use the annotation based approach?
public Object processEvent(#Lookup MuleEventContext eventContext) throws Exception {
System.out.println("eventContext: " + eventContext.getMessage().getPayload());
return "GOT IT";
}
From the documentation:
The Lookup annotation is used to inject objects from the Mule registry
The MuleEventContext is not a registry object but a dynamic one, so this can not work.
What do you need the MuleEventContext for?