我相信我的应用程序出错了。
当我尝试从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。:'...'的一部分,我只是不通过这里的所有数据。
有没有人有同样的问题?似乎有些特征具有不同的编码类型。
Edit.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
*/
Edit.2:
出于某种原因,问题再次发生。
有没有办法解决这个问题?
答案 0 :(得分:1)
一种解决方案是创建自定义策略插件并覆盖doApply
方法,以便在使用Transfer-Encoding
对象等于chunked
时删除ApiResponse
标头。
apiresponse.getHeaders().remove("Transfer-Encoding");
并将新的自定义策略插件添加到您的服务中。这是一个简单的tutorial来创建插件