BATMAN_2008
BATMAN_2008

Reputation: 3530

Why does Quarkus @Produces(MediaType.APPLICATION_JSON) automatically add the array wrapper and comma to values?

I have a REST API developed using Quarkus with @Produces(MediaType.APPLICATION_JSON). This method returns the list of customers asynchronously.

But the problem is that it adds the additional array wrapper and comma separation, which is not required. Everything works fine when used with MediaType.TEXT_PLAIN but since my response is of JSON type I want to keep MediaType.APPLICATION_JSON.

Is there anyway to avoid the modification to response?

For example following is the response I am getting with @Produces(MediaType.APPLICATION_JSON). You can see it adds unwanted commas (e.g. in customerList) and arrays within my response:

[
  {
  "isA": "customerDocument",
  "createdOn": "2022-10-10T12:29:43",
  "customerBody": {
    "customerList": [,
      {
        "name": "Batman",
        "age": 45,
        "city": "gotham"
      },
      {
        "name": "superman",
        "age": 50,
        "city": "moon"
      },
    ]
  }
}]

The response I would like is correctly achievable with MediaType.TEXT_PLAIN:

{
  "isA": "customerDocument",
  "createdOn": "2022-10-10T12:29:43",
  "customerBody": {
    "customerList": [
      {
        "name": "Batman",
        "age": 45,
        "city": "gotham"
      },
      {
        "name": "superman",
        "age": 50,
        "city": "moon"
      }
    ]
  }
}

When I use text/plain, everything works fine. Is there something that I can modify to avoid the addition of array wrapper and comma when using with application/json?

I believe this is happening because I am generating the elements in customerList asynchronously using the SmallRye Mutin Multi<String>. Can someone please provide some suggestion?

The following is the Quarkus REST API for GET requests:

  @Path("/testData")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Multi <String> test() throws IOException {
      return Multi.createFrom().items(IOUtils.toString(getClass().getResourceAsStream("/TestJSON.json"), StandardCharsets.UTF_8));
  }

Upvotes: 1

Views: 1266

Answers (0)

Related Questions