Azure / azure-functions-java-library

Contains annotations for writing Azure Functions in Java
MIT License
43 stars 43 forks source link

@EventHubTrigger cannot deserialize HttpResponseMessage. What is best way to deserialize HttpResponseMessage? #74

Open tarinidash opened 5 years ago

tarinidash commented 5 years ago

I have a function(@HttpTrigger) which writes HttpResponseMessage to email topic. I am trying to create another function(@EventHubTrigger) to read message from this topic.

@FunctionName("EventHubTrigger-EmailCurated")
    public void run(
            @EventHubTrigger(name = "httpResponseMessage", eventHubName = "email", connection = "AzureEventHubConnection") HttpResponseMessage httpResponseMessage,
            final ExecutionContext context) {
        logger = context.getLogger();
        logger.info("EventHubTrigger-EmailCurated received a httpResponseMessage : " + httpResponseMessage);
}
Stack: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.microsoft.azure.functions.HttpResponseMessage. Registering an InstanceCreator with Gson for this type may fix this problem.
Caused by: java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: com.microsoft.azure.functions.HttpResponseMessage

I understand HttpResponseMessage is an interface. Is there any implementation class available that I can cast to?

dcr007 commented 3 years ago

@tarinidash - Did you get to resolve this ? I have a similar issue where , a function generateSplunkData(@HttpTrigger) sends HttpResponseMessage to processSplunkData((@EventHubTrigger) to read the response sent . But I get the following exception .

Exception: UnsupportedOperationException: Interface can't be instantiated! Interface name: com.microsoft.azure.functions.HttpResponseMessage
Stack: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.microsoft.azure.functions.HttpResponseMessage. Registering an InstanceCreator with Gson for this type may fix this problem.
 @FunctionName("processSplunkData")
    public void processSplunkData(
            @EventHubTrigger(
                    name = "msg",
                    eventHubName = "", // blank because the value is included in the connection string
                    cardinality = Cardinality.ONE,
                    connection = "EventHubConnectionString")
                    HttpResponseMessage item,
            @CosmosDBOutput(
                    name = "databaseOutput",
                    databaseName = "TelemetryDb",
                    collectionName = "Reports",
                    connectionStringSetting = "CosmosDBConnectionString")
                    OutputBinding<HttpResponseMessage> document,
            final ExecutionContext context) {
        context.getLogger().info("Event hub message received: " + item.toString());
        if(item!=null)  document.setValue(item);
    }
tarinidash commented 3 years ago

Hey @dcr007 I am assuming in your case there is a @HttpTrigger which creates this HttpResponseMessage which is stored in an eventHub. If I remember correctly, I gave up on this and made my @HttpTrigger to return a custom class/Bean. I was able to read and deserialize this custom class from @EventHubTrigger

@FunctionName("HttpTrigger-Sample")
    @EventHubOutput(name = "message",
            eventHubName = "%eventHubName%",
            connection = "AzureEventHubConnection")
    public final CustomResponseBody run(
            @HttpTrigger(name = "httpRequestMessage",
                    methods = {HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS) final HttpRequestMessage<Optional<CustomRequest>> httpRequestMessage,
            final ExecutionContext context) {
        Assert.notNull(httpRequestMessage, "httpRequestMessage cannot be null");
        logger = context.getLogger();
        CustomResponseBody responseBody =. getCustomResponse(logger, httpRequestMessage);
        return responseBody;
    }

In the above case CustomResponseBody is a custom Java Bean that I created. Hope it helps.

Thanks

dcr007 commented 3 years ago

hey tarinidash - thanks ! So does the CustomResponseBody implements HttpResponseMessage ?