Set response content from java callout

Not applicable

I'm making endpoint call from within java callout and trying to set the responset in my callout. I get a NullPointerException when I access MessageContext.getResponseMessage().setContent(mycontent). According to docurmentation (http://docs.apigee.com/api-services/reference/variables-reference), this variable is not in scope until Target response. Since I do not have a target, how can I set this variable?

Version 4.16.09.02

Solved Solved
1 2 585
1 ACCEPTED SOLUTION

It's likely that the Java callout is running in the request flow.

In this case there is no Response Message, and so getResponseMessage() is returning null.

You are then de-referencing that null pointer, resulting in the NPE.

possible Solutions or avoidances (choose one)

  1. run the Java callout in the Response flow
  2. make the Java callout smart enough to handle the case where the Response Message does not yet exist. Set the content in "message.content" rather than in getResponseMessage().
    MessageContext.setVariable("message.content", mycontent);

The second approach is probably preferred because it makes your java callout more resilient in general.

View solution in original post

2 REPLIES 2

It's likely that the Java callout is running in the request flow.

In this case there is no Response Message, and so getResponseMessage() is returning null.

You are then de-referencing that null pointer, resulting in the NPE.

possible Solutions or avoidances (choose one)

  1. run the Java callout in the Response flow
  2. make the Java callout smart enough to handle the case where the Response Message does not yet exist. Set the content in "message.content" rather than in getResponseMessage().
    MessageContext.setVariable("message.content", mycontent);

The second approach is probably preferred because it makes your java callout more resilient in general.

Tried #2 above, worked great.