java—编写灵活、干净的方法来处理spring中的多个RESTAPI调用

63lcw9qa  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(109)

为了解决这个问题,假设我们有一个下游服务,我们需要调用它来获取一些信息。我们设置一个api端点并调用另一个方法,该方法将保存我们的业务逻辑并发出http请求。根据发送给这个方法的某些参数,我们可能需要对同一个端点进行多次调用,这取决于它返回的内容。方法当前设置如下:

public HttpEntity<String> getInfo(//parameters) {

    //setup HTTP headers etc.
    HttpEntity response = restTemplate.exchange(//stuff here);

    //based off of on the parameters given to this method, we will know whether or not we need to make additional calls
    //if we DO need to make additional calls, we will also need to inspect the response for information
    //this is difficult, because as you see below, this method doesn't start to procces the response until after error checking

    //do all error checking ie. checking for no data and other HTTP errors

    OurDataClass dataClass = objectmapper.writeValueasString(response.getBody());
    //do things to dataClass
    return new HttpEntity<String>(//dataClass and headers);

鉴于此方法的当前结构,我不知道如何将其转化为更具可扩展性和可维护性的内容。我的第一React是封装restemplate并在那里处理其他调用,但是考虑到我需要检查每个调用的请求内容,这是在当前方法结束前才完成的,所以看起来我们在做大量的双重工作。另一方面,在没有任何封装的情况下将解决方案应用到方法中会使维护变得更加困难(错误检查已经有点混乱)。
我是不是想得太多了?试图在这里嵌入某种设计模式是错误的吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题