有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建一个通用服务来处理不同的JSON POJO请求、执行客户端调用并返回它们各自的响应?

目前,我们正在创建不同的服务来处理不同的JSON Pojo请求。这些服务中的每一个都是定制来构建json请求的,因为每个请求都有不同的参数,如代码所示。 我想要什么? 避免代码重复 创建一个可以处理不同json请求的服务,基于请求体构造json负载,根据请求自动添加参数并执行http客户端调用,我如何做到这一点

通过我在代码中显示的导入,您可以看到我正在使用哪个json对象

import org.json.JSONObject;


public class GoogleSearchService {
    public GoogleSearchResponse getGoogleSearchResult(GoogleSearchRequest request) {

    Response response = customHttpclientService.post("googleUrl")
                 .headers("added headers here")
                 .requestBody(MediaType.application_json, buildJsonObject().toString())
                 .execute();
    /*The getGoogleSearchRespons() method will parse the response 
    from the response object.
    */
    return getGoogleSearchResponse(response);

}

private JSONObject buildJsonObject(GoogleSearchRequest request) {

    JSONObject object = new JSONObject():
    object.put("paramOne", request.getParamOne());
    //And so on all params are added like this in each service
}

}

共 (0) 个答案