Home > Software engineering >  quarkus return String
quarkus return String

Time:09-02

 String s=  entityService.createEntry(multimaparr, resolved).onItem().transformToUni(createResult ->{
            String entityId = createResult.getEntityId();
        });

this is my code I want to return String like entityId but it is showing an error, in the transform, is there any way to get String Entity Id in quarkus?

CodePudding user response:

I'm assuming createEntry() returns a reactive Mutiny type (i.e. Uni or Multi).

So lets say your createEntry() returns a Uni object, when you call .onItem(), that allows you to do something with the Entry object that will be emitted by the Uni. In your example you have transformToUni(); the parameter to that function should be a function that returns a Uni<?>. What you have provided is a function that returns void (i.e. there is no return). What you COULD do is have your code inside your lambda be:

String s = createResult.getEntityId();
return Uni.createFrom().item(s);

This would return a Uni to your transformToUni parameter.

You could do even one better by doing the following:

Uni<String> s = entityService.createEntry(multimaparr, resolved).onItem().transform(createResult -> createResult.getEntityId());

Now, how do you get the string out of your Uni? Two ways:

  1. Fully embrace reactive types end-to-end. Assuming you're making an API, set the return type of you endpoint to be a Uni<?> where ? is whatever type you want to express
  2. The Uni Javadoc documents the "await" method. The return type of await() allows you to get the item emitted by the Uni by either waiting for a pre-determined amount of time (think of a timeout) or indefinitely (not recommended because of the halting problem).

CodePudding user response:

Thank you for giving a very good exposure.

But I am performing Junit Testing for the Service class. so in my service class return Uni object . Then I want get the entity id from Uni object after that comparing both entities if I will be proving to like to this Assert.assertEquals("urn:ngsi-ld:Vehicle:A103", id)

**String id = entityService.createEntry(multimaparr, resolved);
                Assert.assertEquals("urn:ngsi-ld:Vehicle:A103", id);**
  • Related