How to change the structure of a static method at runtime?

Asked

Viewed 76 times

1

I am building a Java application that consumes a REST Web Service. I sought to use the new Httpclient of Java 11. However, the same request in this target application sometimes gets a post, sometimes gets a GET but with the same head changing only the method.

In the examples I found on the Internet I invoke the Httprequest method and build it with . Method1(). Methodo2() and so on. So in my case of use as sometimes I have GET SOMETIMES POST but the header and URL are the same, I need to do the following:

private HttpResponse<String> getResponse() throws IOException, InterruptedException {

    HttpRequest request;

    if(post) {
        request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .POST(HttpRequest.BodyPublishers.ofString(payload))
                .header("Content-Type", "application/json")
                .build();

    } else {
        request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .header("Content-Type", "application/json")
                .build();
    }

    return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

I’m doing an IF and having to repeat the same whole structure, I would have like to intervene in the middle of it with kind of a ternary to have only one method?

Important note: even if it is possible to use Httpclient / Httprequest otherwise I would like it to be preserved in this way even so that I can understand better, because it is not the first example of the genre I see, I’ve seen the same scenario in Twilio ML using this "Builder" to build XML, and there a same parameter can be repeated, example:

Message message = new Message.Builder().body(body).body(body1).media(media).build();

What if I wanted to repeat N times in the case of Twilio? How would I?

It has a name for this method chain structure?

1 answer

0


If I understand what you want, you don’t. You want a ternary that calls distinct methods according to one condition, right? Java can’t stand it, at least I’ve never seen it.

This string of methods comes from a design pattern called Builder. He partially mounts the object until the call ends build(), which returns the mounted object.

A similar concept is that of interface Fluent, you can search around for examples using this term.

If I wanted to repeat the header three times, I would do so:

HttpRequest.newBuilder()
            .uri(URI.create(url))              
.POST(HttpRequest.BodyPublishers.ofString(payload))
            .header("Content-Type", "application/json")
            .header("outra chave", "outro valor")
            .header("mais uma chave", "mais um valor")
            .build();

Edited:

A feature of the design pattern Builder is that while the object is incomplete, its preparation methods such as the header() return the respective "Builder", which we can call a partially finished object. This object does not belong to the class HttpRequest but to an auxiliary class (or rather, interface), the HttpRequest.Builder. Then, when it’s ready, it’s called the method build() of that "Builder" and the ready object is delivered to you.

It is not very clear why you only know the number of headers during the run, but as you are in that situation, you can save the "Builder" partially ready and call it as you discover there are new headers to add. The "Builder" is the object that is returned by the method header() and other methods of HttpRequest.

I don’t put example in code because I didn’t fully understand the scenario you have there.

  • Piovezan thanks for the quick reply. It is well aligned with what I am waiting for, now with the name of Pattern I am already researching on the subject to specialize more. But there is still a doubt, using the same example you gave, if I know the amount of header only during execution, as I would add several . header (according to demand). Starting from the premise that there is no . headers method (with s at the end) in this example. How to encapsulate this in my class since there is no ternary for this?

  • I edited the answer.

  • Thanks! Guided me a lot with respect to the Builder :D pattern

Browser other questions tagged

You are not signed in. Login or sign up in order to post.