Probleme mit einer REST -API, die die Antwort ständig ändertJava

Java-Forum
Anonymous
 Probleme mit einer REST -API, die die Antwort ständig ändert

Post by Anonymous »

  • Was ich erreichen möchte: < /li>
    < /ul>
    Ich möchte mit dem HTTP -Antwortkörperobjekt einer API von Drittanbietern auf dem Laufenden halten, das sich ständig ändert. Damit meine ich:

    Hintergrund:

    Angenommen, ein Szenario mit drei Webdiensten. Ich habe überhaupt keine Kontrolle über diesen Webservice. Die HTTP -Objektantwort ändert sich jedoch immer wieder, dh eine Minute zuvor enthält sie eine Reihe von Feldern. In der nächsten Minute gibt es neue Felder und die Minute danach noch mehr (oder weniger) Felder.

    Code: Select all

    {
    "firstFieldWillNeverChange": "LLvtJ",
    "thisFieldMightDisappear": "INluQ",
    "someThirdField": 42
    }
    < /code>
    Minute 2: < /p>
    {
    "firstFieldWillNeverChange": "hUTne",
    "someThirdField": 42
    }
    < /code>
    Minute 3: < /p>
    {
    "firstFieldWillNeverChange": "yfrNO",
    "thisFieldMightDisappear": "a1Byv",
    "someThirdField": 42,
    "someFourthFieldPleaseUncommentMe": "nFwiS"
    }
    
    WebService c , genannt careaboutallfield , ist auch ein Webservice von einem anderen Unternehmen. Ich habe keine Kontrolle über. Die gleichen Felder sollten erhalten werden, keine Felder mehr, die nicht weniger Felder, nur die Felderwerte angereichert sind.public record AlwaysChangingObject(
    String firstFieldWillNeverChange,
    String thisFieldMightDisappear,
    int someThirdField
    // ,String someFourthFieldPleaseUncommentMe
    ) {
    }
    < /code>
    import org.springframework.web.bind.annotation.*;

    import java.security.SecureRandom;
    import java.util.Random;

    @RestController
    public class AlwaysChangingController {

    private final Random RANDOM = new SecureRandom();

    private String justToGetSomeValuePleaseIgnore(int length) {
    StringBuilder buffer = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
    String ALPHABET = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
    buffer.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
    }
    return new String(buffer);
    }

    @GetMapping(value = "/alwaysChanging")
    AlwaysChangingObject test(@RequestParam(value = "id", required = true) int id) {
    // will return some pojo based on the ID, but the structure of the returned pojo is always changing.
    // every now and then, fields are being added, removed, modified.
    // This service is doing many deployments per hour. With it, the pojo changes as well
    AlwaysChangingObject alwaysChangingObject = new AlwaysChangingObject(justToGetSomeValuePleaseIgnore(5), justToGetSomeValuePleaseIgnore(5), 42, justToGetSomeValuePleaseIgnore(5));
    return alwaysChangingObject;
    }

    }
    < /code>
    Service C:
    import org.springframework.web.bind.annotation.*;

    import java.util.Map;

    @RestController
    public class CareAboutAllFieldsController {

    @PostMapping(value = "/test")
    String test(@RequestBody Map shouldGetTheLatestCorrectsFieldsNoMoreNoLess) {
    //do some business logic with all the fields, but it needs all of them correctly matching
    System.out.println(shouldGetTheLatestCorrectsFieldsNoMoreNoLess);
    return "OK";
    }

    }
    < /code>
    On my side, I am Webservice B, BehindService. My goal is to keep up on the structure on this always changing service A, but apply enrichment on the fields I can.
    Then, I need to send the enriched object to service C.
    The issue is that when service A changes, I need to also change my object (then redeploy). I cannot keep up, as we are independent services.
  • Here is what I tried:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BehindServiceApplication {

public static void main(final String[] args) {
SpringApplication.run(BehindServiceApplication.class);
}

}

< /code>
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;

@RestController
public class BehindController {

private final RestClient restClient;

public BehindController(RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder.build();
}

@GetMapping(value = "/test")
String test(@RequestParam(value = "id", required = true) int id) {
// 1. get the pojo by ID, but the filds are always changing. Some got added, removed or changed
NotUpToDatePojo notUpToDatePojo = getTheLatestPojoWithLatestFields(id);
System.out.println("I received " + notUpToDatePojo + " but it might probably not be the most up to date object already ");

// 2. do some internal logic on the fields I know. But I should keep all fields
System.out.println("I am doing some business loggic to fields that I know I needto do business logic upon");
NotUpToDatePojo enrichedNotUpToDatePojo = enrich(notUpToDatePojo);

// 3. send the after logic object to another service
System.out.println("complete" + enrichedNotUpToDatePojo);
String response = sendObjectToC(notUpToDatePojo);
return response;
}

private NotUpToDatePojo enrich(NotUpToDatePojo notUpToDatePojo) {
String firstFieldEnriched = null;
if (notUpToDatePojo.firstFieldWillNeverChange() != null) {
firstFieldEnriched = notUpToDatePojo.firstFieldWillNeverChange().toUpperCase();
}

String secondFieldEnriched = null;
if (notUpToDatePojo.thisFieldMightDisappear() != null) {
secondFieldEnriched = notUpToDatePojo.thisFieldMightDisappear().replaceAll("a", "aaa");
}

int thirdField = 0;
if (notUpToDatePojo.someThirdField() != 0) {
thirdField = notUpToDatePojo.someThirdField() * 3;
}

NotUpToDatePojo enrichedPojoWhichMightNotContainAllTheNeededFieldsOrHasTooManyFields = new NotUpToDatePojo(firstFieldEnriched, secondFieldEnriched, thirdField);
return enrichedPojoWhichMightNotContainAllTheNeededFieldsOrHasTooManyFields;
}

private NotUpToDatePojo getTheLatestPojoWithLatestFields(int id) {
return restClient.get()
.uri("http://localhost:8080/alwaysChanging?id=" + id)
.retrieve()
.body(NotUpToDatePojo.class);
}

private String sendObjectToC(NotUpToDatePojo notUpToDatePojo) {
return restClient.post()
.uri("http://localhost:8082/test")
.body(notUpToDatePojo)
.retrieve()
.body(String.class);
}

}
< /code>
public record NotUpToDatePojo(
String firstFieldWillNeverChange,
String thisFieldMightDisappear,
int someThirdField
) {
}
< /code>
The curent approach does not work, as it requires me to keep changing and redeploying NotUpToDatePojo< /code> und dies ist immer zurückgefallen. HTTP -Antwortkörper?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post