根据响应调用Spring控制器

时间:2016-11-01 18:50:15

标签: rest spring-mvc java-ee spring-boot

我们有一个要求......当我们向控制器发出请求时,我们调用另一个api并根据其他api响应我们必须将请求重新触发到具有不同参数的控制器

@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
public Map<String, Account> searchEndpoint(@RequestParam(name = "query", required = true) String query) {

        RequestPayload req = new RequestPayload(query);
        SearchResponse searchResponse = http.executeSecurePost(this.searchUrl, req, SearchResponse.class);

        if(("particularModel").equalsIgnoreCase(searchResponse.getModel()))
        {
              String  differentQueryParameter = searchResponse.getName();
              searchEndpoint(differentQueryParameter);
        } 
        else {
             //do something else
        }
        return json response;

}

当我这样做的时候...它正在进行第二次调用并获得新响应并返回...但是再次进行第三次查询,其中这次是原始查询被解雇了。不确定为什么会触发第三个查询。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

一次又一次地调用处理程序可能会导致递归函数。你可以避免这种编程。你在服务方法中调用api的逻辑就行了。

@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")public Map<String, Account> searchEndpoint(@RequestParam(name = "query", required = true) String query) {
  //put this logic in service
  RequestPayload req = new RequestPayload(query);
  SearchResponse searchResponse = http.executeSecurePost(this.searchUrl, req, SearchResponse.class);

    if(("particularModel").equalsIgnoreCase(searchResponse.getModel()))
    {
          String  differentQueryParameter = searchResponse.getName();
          //put this logic in service and just call it
          RequestPayload req = new RequestPayload(query);
          SearchResponse searchResponse = http.executeSecurePost(this.searchUrl, req, SearchResponse.class)
    } 
    else {
         //do something else
    }
    return json response;

}