REST服务未发送回预期的响应

时间:2019-12-20 21:01:52

标签: java rest http

这是我的情况。

我有一个来自我们UI的REST调用,该调用首先转到JBoss服务器上的Java方法以开始处理。在这种方法中,我需要再次调用另一个REST服务...这次是在ICE实例上。

正在发生的事情是,从ICE实例到JBoss端,一切都会按预期返回,但是响应并没有完全返回到UI。我期望收到JSON响应,但是由于某种原因,它看起来像发起调用正在返回HTTP文本状态“确定”,而不是JSON。

我尝试在第一种方法中对一个虚拟响应进行硬编码,但仍然没有得到回复。

对此有什么看法吗?

这是JBOSS功能:

@ResponseBody
public Response updateVendorStatusServiceIntoErailSafe(UpdateRequest constructRequestObject) {
    LOGGER.info("Before SM Cookie");
    SmClient client = smAuthentication.getSmCookie();
    LOGGER.info("COOKIE: " + client.getCookie());
    LOGGER.info("After SM Cookie");
    String url = "https://www.fake.com";
    List<NameValuePair> headers = new ArrayList<NameValuePair>();
    headers.add(new BasicNameValuePair("Accept", "application/json"));
    headers.add(new BasicNameValuePair("Content-Type", "application/json"));
    String s = client.makeJsonRequest(url, convertJsonStringFromObject(constructRequestObject), headers, TIMEOUT, "POST");
    LOGGER.info("After Service Call");
    LOGGER.info("RETURN STRING: " + s);
    try
    {
        JSONObject i = new JSONObject(s);
        JSONObject j = i.getJSONObject("response");
        Response r = new Response();
        if(!j.isNull("result"))
        {
            r.setResult(j.getString("result"));
        }
        if(!j.isNull("e"))
        {
            r.setE(j.getString("e"));
        }
        if(!j.isNull("reason"))
        {
            r.setReason(j.getString("reason"));
        }
        if(!j.isNull("stacktrace"))
        {
            r.setStacktrace(j.getString("stacktrace"));
        }
        LOGGER.info("SEND RESPONSE");
        return r;
    }
    catch(Exception e)
    {
        LOGGER.info("EXCEPTION");
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String sStackTrace = sw.toString(); // stack trace as a string
        LOGGER.error(sStackTrace);
        return null;
    }

}

以下是对上述代码进行首次调用的JavaScript代码:

postDataToService: function(url, reqData, fnSussErrorCbk, bIsAsync) {
        var _this = this;
        if (reqData === null || reqData === undefined) {
            reqData = {};
        }
        var resultantData = [];
        $.ajax({
            async: true,
            url: this.getJavaServiceUrl(url),
            type: 'POST',
            dataType: "json",
            data: JSON.stringify(reqData),
            accepts: {json: 'application/json'},
            beforeSend: function(xhr) {
                if (_this.gvtoken) {
                    xhr.setRequestHeader("X-CSRF-Token", _this.gvtoken);
                    xhr.setRequestHeader("response", "Fetch");
                }
            },
            contentType: "application/json",
            mimeType: 'application/json',
            cache: false,
            success: function(data, textStatus, xhr) {
                console.log(textStatus);
                if (xhr.status === 200 && textStatus === "success" || textStatus === "OK" || xhr.status === 201) {
                    resultantData = data;
                    console.log(data);
                }
                var response = xhr.getResponseHeader("response");
                var responseMessage = JSON.parse(response);
                this.gvsucessMsg = responseMessage ? responseMessage.desc : '';
                this.gvsucessStatus = responseMessage ? responseMessage.success : '';
                fnSussErrorCbk && fnSussErrorCbk(resultantData);
            },
            error: function(err, textStatus, xhr) {
                if(err && typeof(err.responseText) == "string" && err.responseText.indexOf(">") > -1 && err.responseText.indexOf("<") > -1){
                    var result = err.responseText.match(/<title>(.*?)<\/title>/g);
                    result = result ? result[0].replace(/<\/?title>/g,'') : err.responseText;
                    resultantData = result;
                }
                else if (err && typeof(err.responseText) == "string" && err.responseText.indexOf("{") > -1 && err.responseText.indexOf(">") === -1) {
                    resultantData = JSON.parse(err.responseText);
                } else if (err && typeof(err.responseText) == "string") {
                    resultantData = err.responseText;
                    if (err.statusText && (resultantData === '' || resultantData.indexOf(">") > -1)) {
                        resultantData = err.statusText;
                    }
                } else {
                    resultantData = err;
                }
                fnSussErrorCbk && fnSussErrorCbk(resultantData);
            }
        });
    },

这是我从ICE端得到的响应,应该一直返回到UI:

返回字符串:{\“ response \”:{\“ e \”:null,\“ reason \”:null,\“ stacktrace \”:null,\“ result \”:\“ Updated 1 records \ “}}

0 个答案:

没有答案