0

我相信我的应用程序出错了。

当我尝试从 url 检索信息时,JSON 对象出现错误。出现这种情况是由于某种原因,APIMan 在响应中包含了一种编码。这是我的代码:

Client client = Client.create();
WebResource webResource = client.resource(uri);
ClientResponse response = webResource.header("X-API-Key", xApiKey)
    .header("Content-Type", "application/json; charset=UTF-8")
    .header("Accept", "application/json")
    .header("Authorization", "Bearer " + token)
    .get(ClientResponse.class);
String json = response.getEntity(String.class);

当我通过 apiman 发送请求时,我的响应以特殊字符显示:

public static void main(String[] args) {
    String uri = "https://apiman.url/apiman-gateway/org/api/1.0/method";
    String apiKey = "894fd5b3-36f0-32974-xxxxxxxx"; 
    get("token", uri, apiKey);
}

// response is 1ff8\r\n{"data":...1ff8\r\nSim"[9867,"CAR\r\n1c7d\r\n...}\r\n0\r\n\r\n

当我不使用 apiman 时,我的反应会有所不同,如下所示:

public static void main(String[] args) {
    String uri = "https://192.168.56.22:8080/app/method";
    String apiKey = "not needed"; 
    get("token", uri, apiKey);
}

// response is ok {"data":...Sim"[9867,"CAR...}

PS.:'...'的部分只是我没有在这里传递所有数据。

有没有人有同样的问题?似乎某些字符具有不同的编码类型。

编辑1:

如果我尝试直接访问 apiman url,则显示响应正常:

https://apiman.url/apiman-gateway/org/api/1.0/method?apikey=894fd5b3-36f0-32974-xxxxxxxx

// response is ok {"data":...Sim"[9867,"CAR...}

在另一个测试中,我使用了 cUrl 方法:

curl -i -X GET -H "Content-Type:application/json" -H "X-API-Key:894fd5b3-36f0-32974-xxxxxxxx" 'https://apiman.url/apiman-gateway/org/api/1.0/method'

/* response is 1ff8
{"data":...
1ff8
Sim"[9867,"CAR
1c7d
...}
0

*/

编辑2:

由于某种原因,问题再次出现。

有没有办法解决这个问题?

4

1 回答 1

1

一种解决方案是创建自定义策略插件并覆盖doApply方法以删除Transfer-Encoding标头(如果它等于chunked使用ApiResponse对象)。

apiresponse.getHeaders().remove("Transfer-Encoding");

并将新的自定义策略插件添加到您的服务中。这是一个创建插件的简单教程

于 2017-01-03T16:39:19.183 回答