1

I want to send a DTO from one quarkus application to another. I am using quarkus as framework. I am looking for something similar to this Send a simple POST request from Quarkus/Java
This is for POST how do I implement what JCompetence is doing in the above post's answer using GET.

I am very new to quarkus and REST. What will be my files in both the projects?
What is the flow of your code when using REST.

A very simple implementation/demonstration of how to send a DTO from one application will be very helpful. Thank you in advance. It will help me a lot.

1 Answers1

0

@Abdullah Chaudhry although is possible to send a body (your DTO) in a GET request, I believe it's not recommended. This post - HTTP GET with request body - talks about this. But if you want to do this anyway, the ideia is exacly the same as the POST example you gave. You only need to change from POST to GET.

But, if you're talking about returning a DTO on the body of a GET response, I guess is OK.

Below I put one example using the Quarkus 2.11.1.Final version.

This example shows how to send a GET with a body and how to answer a GET with another body.

You can find help on these Quarkus guides:

DTO

These classes will exist on the Server and Client sides, they will be your DTOs in this example:

public class Account {
    public double balance;
}
public class BankClient {
    public int age;
    public String name;
}

Server side

pom.xml

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
    </dependency>

resource

@Path("/client")
public class ClientResource {
    @Path("/account")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAccount(BankClient bankClient) {
        var account = new Account();
        account.balance = 123.45;
        return Response.status(200).entity(account).build();
    }
}

Client side

pom.xml

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-rest-client-reactive</artifactId>
    </dependency>

application.properties

quarkus.rest-client.client-api.url=http://localhost:8080
quarkus.rest-client.client-api.scope=javax.inject.Singleton

Rest client service

package org.acme;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("/client")
@RegisterRestClient(configKey="client-api")
public interface ClientService {

    @Path("/account")
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    Account getByBankClient(BankClient bankClient);

}

Tester

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RestClient;
@Path("/test")
public class ClientResource {

    @Inject
    @RestClient
    ClientService clientService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response consumeAccount() {

        var bankClient = new BankClient();
        bankClient.age = 30;
        bankClient.name = "John";

        var account = clientService.getByBankClient(bankClient);

        return Response.status(200).entity(account).build();
    }
}

Executing the test

  1. Start the Server side application

  2. Start the Client side application

  3. Execute the /test of the Client side, for example with:

$ curl --verbose http://localhost:8090/test

The response should be:

*   Trying 127.0.0.1:8090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8090 (#0)
> GET /test HTTP/1.1
> Host: localhost:8090
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
< content-length: 18
< 
* Connection #0 to host localhost left intact
{"balance":123.45}
Felipe Windmoller
  • 1,528
  • 1
  • 12
  • 24